Update day 15 solution
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user