From 1255dab24fb306755026eb1fb4715aeedc2a199a Mon Sep 17 00:00:00 2001 From: Pascal Lais Date: Wed, 15 Dec 2021 19:36:48 +0100 Subject: [PATCH] Update day 15 solution --- day-15/day-15.py | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/day-15/day-15.py b/day-15/day-15.py index 8a97582..db259bf 100644 --- a/day-15/day-15.py +++ b/day-15/day-15.py @@ -42,17 +42,8 @@ def part_2(input): x_size = len(input[0].strip()) y_size = len(input) repeat = 5 - nodes = {} - for y, line in enumerate(input): - for x, v in enumerate(line.strip()): - for i in range(repeat): - for j in range(repeat): - xi = x + (x_size * i) - yj = y + (y_size * j) - vi = (int(v) + i + j) - if 10 <= vi: - vi -= 9 - nodes[(xi, yj)] = vi + nodes = {(x, y): int(v) for y, line in enumerate(input) + for x, v in enumerate(line.strip())} start = (0, 0) end = ((x_size * repeat) - 1, (y_size * repeat) - 1) cost = {start: 0} @@ -74,11 +65,14 @@ def part_2(input): if 0 <= x < (x_size * repeat) and 0 <= y < (y_size * repeat): if not (x, y) in done: next.add((x, y)) - for n in next: - next_cost = cur_cost + nodes[n] - if not n in cost or next_cost < cost[n]: - cost[n] = next_cost - queue.add(n) + for (x, y) in next: + next_cost = nodes[(x % x_size, y % y_size)] + \ + (x // x_size) + (y // y_size) + if next_cost > 9: + next_cost -= 9 + if not (x, y) in cost or (cur_cost + next_cost) < cost[(x, y)]: + cost[(x, y)] = cur_cost + next_cost + queue.add((x, y)) done.add(current) result = cost[end] print("Part 2 result:", result)