Day 12 (minified)
This commit is contained in:
30
day12/part1.min.py
Normal file
30
day12/part1.min.py
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
lines = np.array([np.array([ord(y) for y in x.strip()]) for x in open("input.txt")])
|
||||||
|
start = tuple(np.array(np.where(lines == ord('S'))).T[0])
|
||||||
|
end = tuple(np.array(np.where(lines == ord('E'))).T[0])
|
||||||
|
|
||||||
|
lines[start] = ord('a')
|
||||||
|
lines[end] = ord('z')
|
||||||
|
lines = lines-ord('a')
|
||||||
|
|
||||||
|
|
||||||
|
open = [(0, start, None)]
|
||||||
|
closed = dict()
|
||||||
|
while open:
|
||||||
|
prio, n, prev = open.pop(0)
|
||||||
|
if n in closed:
|
||||||
|
continue
|
||||||
|
closed[n] = (prio, prev)
|
||||||
|
if n == end:
|
||||||
|
path = [n]
|
||||||
|
while path[-1] is not None:
|
||||||
|
path.append(closed[path[-1]][1])
|
||||||
|
print(len(path)-2)
|
||||||
|
break
|
||||||
|
|
||||||
|
for dx, dy in [(-1, 0), (0, -1), (1, 0), (0, 1)]:
|
||||||
|
xn, yn = n[0]+dx, n[1]+dy
|
||||||
|
if 0 <= xn < len(lines) and 0 <= yn < len(lines[0]) and lines[n] + 1 >= lines[xn, yn]:
|
||||||
|
open.append((prio+1, (xn, yn), n))
|
||||||
@@ -53,8 +53,6 @@ def find_path(start, end_fnct, neighbors_fnct, heuristic_cost_estimate_fnct, dis
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
path = list(find_path(
|
path = list(find_path(
|
||||||
start,
|
start,
|
||||||
end_fnct=is_end,
|
end_fnct=is_end,
|
||||||
|
|||||||
30
day12/part2.min.py
Normal file
30
day12/part2.min.py
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
lines = np.array([np.array([ord(y) for y in x.strip()]) for x in open("input.txt")])
|
||||||
|
start = tuple(np.array(np.where(lines == ord('S'))).T[0])
|
||||||
|
end = tuple(np.array(np.where(lines == ord('E'))).T[0])
|
||||||
|
|
||||||
|
lines[start] = ord('a')
|
||||||
|
lines[end] = ord('z')
|
||||||
|
lines = lines-ord('a')
|
||||||
|
|
||||||
|
|
||||||
|
open = [(0, end, None)]
|
||||||
|
closed = dict()
|
||||||
|
while open:
|
||||||
|
prio, n, prev = open.pop(0)
|
||||||
|
if n in closed:
|
||||||
|
continue
|
||||||
|
closed[n] = (prio, prev)
|
||||||
|
if lines[n] == 0:
|
||||||
|
path = [n]
|
||||||
|
while path[-1] is not None:
|
||||||
|
path.append(closed[path[-1]][1])
|
||||||
|
print(len(path)-2)
|
||||||
|
break
|
||||||
|
|
||||||
|
for dx, dy in [(-1, 0), (0, -1), (1, 0), (0, 1)]:
|
||||||
|
xn, yn = n[0]+dx, n[1]+dy
|
||||||
|
if 0 <= xn < len(lines) and 0 <= yn < len(lines[0]) and lines[n] - 1 <= lines[xn, yn]:
|
||||||
|
open.append((prio+1, (xn, yn), n))
|
||||||
@@ -53,8 +53,6 @@ def find_path(start, end_fnct, neighbors_fnct, heuristic_cost_estimate_fnct, dis
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
path = list(find_path(
|
path = list(find_path(
|
||||||
end,
|
end,
|
||||||
end_fnct=is_end,
|
end_fnct=is_end,
|
||||||
|
|||||||
Reference in New Issue
Block a user