From 18c76f694b31d02b6de7a4b554fb5df73ab39b76 Mon Sep 17 00:00:00 2001 From: Sebastian Seedorf Date: Thu, 16 Dec 2021 10:03:16 +0100 Subject: [PATCH] Day 15 (speedup) --- day15/part1.py | 33 ++++++++++++++++++--------------- day15/part2.py | 38 +++++++++++++++++++++----------------- 2 files changed, 39 insertions(+), 32 deletions(-) diff --git a/day15/part1.py b/day15/part1.py index f5352d4..4a4daa5 100644 --- a/day15/part1.py +++ b/day15/part1.py @@ -4,22 +4,25 @@ import heapq import numpy as np arr = np.array([[int(n) for n in line.strip()] for line in open("input.txt")]) -arr = np.pad(arr, (1, 1), 'constant', constant_values=(9999,)) -open_list = [(arr[1, 1], (1, 1))] -closed_list = np.zeros_like(arr) -target = arr[:-2, :-2].shape +open_list = [(arr[0, 0], (0, 0))] +closed_list = set() +target = ty, tx = arr[:-1, :-1].shape while len(open_list): - val, (y, x) = heapq.heappop(open_list) - if (y, x) == target: - print(val - arr[1, 1]) - break - if closed_list[y, x] != 0: + val, curr = heapq.heappop(open_list) + if curr in closed_list: continue - closed_list[y, x] = val - for yn, xn in [(1, 0), (-1, 0), (0, 1), (0, -1)]: - if closed_list[y+yn, x+xn] == 0: - heapq.heappush(open_list, (arr[y+yn, x+xn]+val, (y+yn, x+xn))) - -print(np.sum(closed_list[1:-1, 1:-1] != 0), arr[1:-1, 1:-1].shape[0]*arr[1:-1, 1:-1].shape[1]) \ No newline at end of file + if curr == target: + print(val - arr[0, 0]) + break + y, x = curr + closed_list.add(curr) + if 0 < y and (nxt := (y-1, x)) not in closed_list: + heapq.heappush(open_list, (arr[nxt] + val, nxt)) + if 0 < x and (nxt := (y, x-1)) not in closed_list: + heapq.heappush(open_list, (arr[nxt] + val, nxt)) + if ty > y and (nxt := (y+1, x)) not in closed_list: + heapq.heappush(open_list, (arr[nxt] + val, nxt)) + if tx > x and (nxt := (y, x+1)) not in closed_list: + heapq.heappush(open_list, (arr[nxt] + val, nxt)) diff --git a/day15/part2.py b/day15/part2.py index bf5628b..edb4e10 100644 --- a/day15/part2.py +++ b/day15/part2.py @@ -3,25 +3,29 @@ import heapq import numpy as np -arr = np.array([[int(n) for n in line.strip()] for line in open("input.txt")]) -add = np.repeat(np.repeat([list(range(i, i+5)) for i in range(5)], arr.shape[0], axis=0), arr.shape[1], axis=1) +arr = np.array([[int(n) for n in line.strip()] for line in open("input.txt")], dtype='int16') +add = np.array([list(range(i, i+5)) for i in range(5)], dtype='int16') +add = np.repeat(np.repeat(add, arr.shape[0], axis=0), arr.shape[1], axis=1) arr = (np.tile(arr, (5, 5)) + add - 1) % 9 + 1 -arr = np.pad(arr, (1, 1), 'constant', constant_values=(9999,)) -open_list = [(arr[1, 1], (1, 1))] -closed_list = np.zeros_like(arr) -target = arr[:-2, :-2].shape +open_list = [(arr[0, 0], (0, 0))] +closed_list = set() +target = ty, tx = arr[:-1, :-1].shape while len(open_list): - val, (y, x) = heapq.heappop(open_list) - if (y, x) == target: - print(val - arr[1, 1]) - break - if closed_list[y, x] != 0: + val, curr = heapq.heappop(open_list) + if curr in closed_list: continue - closed_list[y, x] = val - for yn, xn in [(1, 0), (-1, 0), (0, 1), (0, -1)]: - if closed_list[y+yn, x+xn] == 0: - heapq.heappush(open_list, (arr[y+yn, x+xn]+val, (y+yn, x+xn))) - -print(np.sum(closed_list[1:-1, 1:-1] != 0), arr[1:-1, 1:-1].shape[0]*arr[1:-1, 1:-1].shape[1]) + if curr == target: + print(val - arr[0, 0]) + break + y, x = curr + closed_list.add(curr) + if 0 < y and (nxt := (y-1, x)) not in closed_list: + heapq.heappush(open_list, (arr[nxt] + val, nxt)) + if 0 < x and (nxt := (y, x-1)) not in closed_list: + heapq.heappush(open_list, (arr[nxt] + val, nxt)) + if ty > y and (nxt := (y+1, x)) not in closed_list: + heapq.heappush(open_list, (arr[nxt] + val, nxt)) + if tx > x and (nxt := (y, x+1)) not in closed_list: + heapq.heappush(open_list, (arr[nxt] + val, nxt))