From 0eb5b5a88f054790fa81e57a1c36a66c2099c731 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=C3=ABl=20Capelle?= Date: Sat, 24 Dec 2022 22:59:40 +0100 Subject: [PATCH] Faster day 24. --- 2022/day24.py | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/2022/day24.py b/2022/day24.py index 95d4fad..975f178 100644 --- a/2022/day24.py +++ b/2022/day24.py @@ -17,7 +17,6 @@ winds = { n_rows, n_cols = len(lines) - 2, len(lines[0]) - 2 CYCLE = math.lcm(n_rows, n_cols) -# for each row, set of forward winds east_winds = [{j for j in range(n_cols) if (i, j, ">") in winds} for i in range(n_rows)] west_winds = [{j for j in range(n_cols) if (i, j, "<") in winds} for i in range(n_rows)] north_winds = [ @@ -32,14 +31,13 @@ def run(start: tuple[int, int], start_cycle: int, end: tuple[int, int]): def heuristic(y: int, x: int) -> int: return abs(end[0] - y) + abs(end[1] - x) - # (distance + heuristic, distance, start_pos) + # (distance + heuristic, distance, (start_pos, cycle)) queue = [(heuristic(start[0], start[1]), 0, ((start[0], start[1]), start_cycle))] visited: set[tuple[tuple[int, int], int]] = set() distances: dict[tuple[int, int], dict[int, int]] = defaultdict(lambda: {}) while queue: _, distance, ((y, x), cycle) = heapq.heappop(queue) - # print(y, x, distance, cycle) if ((y, x), cycle) in visited: continue @@ -68,17 +66,15 @@ def run(start: tuple[int, int], start_cycle: int, end: tuple[int, int]): continue if (ty, tx) != start: - if any(ty == (vy + n_cycle) % n_rows for vy in south_winds[tx]): + if (ty - n_cycle) % n_rows in south_winds[tx]: continue - if any(ty == (vy - n_cycle) % n_rows for vy in north_winds[tx]): + if (ty + n_cycle) % n_rows in north_winds[tx]: continue - if any(tx == (vx - n_cycle) % n_cols for vx in west_winds[ty]): + if (tx + n_cycle) % n_cols in west_winds[ty]: continue - if any(tx == (vx + n_cycle) % n_cols for vx in east_winds[ty]): + if (tx - n_cycle) % n_cols in east_winds[ty]: continue - # print(f"{y} {x} [{cycle}] -> {ty} {tx}") - heapq.heappush( queue, ((heuristic(ty, tx) + distance + 1, distance + 1, ((ty, tx), n_cycle))),