diff --git a/day12/part1.min.py b/day12/part1.min.py new file mode 100644 index 0000000..81d7b23 --- /dev/null +++ b/day12/part1.min.py @@ -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)) diff --git a/day12/part1.py b/day12/part1.py index d6b4db8..c962036 100644 --- a/day12/part1.py +++ b/day12/part1.py @@ -53,8 +53,6 @@ def find_path(start, end_fnct, neighbors_fnct, heuristic_cost_estimate_fnct, dis return None - - path = list(find_path( start, end_fnct=is_end, diff --git a/day12/part2.min.py b/day12/part2.min.py new file mode 100644 index 0000000..3409eaa --- /dev/null +++ b/day12/part2.min.py @@ -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)) diff --git a/day12/part2.py b/day12/part2.py index 0639c2b..fbd0bae 100644 --- a/day12/part2.py +++ b/day12/part2.py @@ -53,8 +53,6 @@ def find_path(start, end_fnct, neighbors_fnct, heuristic_cost_estimate_fnct, dis return None - - path = list(find_path( end, end_fnct=is_end,