Update day 15 solution
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2021-12-15 19:36:48 +01:00
parent a3becde292
commit 1255dab24f

View File

@@ -42,17 +42,8 @@ def part_2(input):
x_size = len(input[0].strip()) x_size = len(input[0].strip())
y_size = len(input) y_size = len(input)
repeat = 5 repeat = 5
nodes = {} nodes = {(x, y): int(v) for y, line in enumerate(input)
for y, line in enumerate(input): for x, v in enumerate(line.strip())}
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
start = (0, 0) start = (0, 0)
end = ((x_size * repeat) - 1, (y_size * repeat) - 1) end = ((x_size * repeat) - 1, (y_size * repeat) - 1)
cost = {start: 0} cost = {start: 0}
@@ -74,11 +65,14 @@ def part_2(input):
if 0 <= x < (x_size * repeat) and 0 <= y < (y_size * repeat): if 0 <= x < (x_size * repeat) and 0 <= y < (y_size * repeat):
if not (x, y) in done: if not (x, y) in done:
next.add((x, y)) next.add((x, y))
for n in next: for (x, y) in next:
next_cost = cur_cost + nodes[n] next_cost = nodes[(x % x_size, y % y_size)] + \
if not n in cost or next_cost < cost[n]: (x // x_size) + (y // y_size)
cost[n] = next_cost if next_cost > 9:
queue.add(n) 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) done.add(current)
result = cost[end] result = cost[end]
print("Part 2 result:", result) print("Part 2 result:", result)