Optimize day 15 solution by using priority queue
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:
@@ -1,5 +1,6 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from heapq import heappush, heappop
|
||||||
|
|
||||||
|
|
||||||
def part_1(input):
|
def part_1(input):
|
||||||
@@ -11,16 +12,13 @@ def part_1(input):
|
|||||||
start = (0, 0)
|
start = (0, 0)
|
||||||
end = (x_size - 1, y_size - 1)
|
end = (x_size - 1, y_size - 1)
|
||||||
cost = {start: 0}
|
cost = {start: 0}
|
||||||
queue = set([start])
|
queue = [(0, start)]
|
||||||
done = set()
|
done = set()
|
||||||
neighbors = set([(-1, 0), (0, -1), (0, 1), (1, 0)])
|
neighbors = set([(-1, 0), (0, -1), (0, 1), (1, 0)])
|
||||||
while len(queue):
|
while len(queue):
|
||||||
cur_cost = None
|
cur_cost, current = heappop(queue)
|
||||||
for n in queue:
|
if current in done:
|
||||||
if cur_cost == None or cost[n] < cur_cost:
|
continue
|
||||||
cur_cost = cost[n]
|
|
||||||
current = n
|
|
||||||
queue.remove(current)
|
|
||||||
(xc, yc) = current
|
(xc, yc) = current
|
||||||
next = set()
|
next = set()
|
||||||
for (xn, yn) in neighbors:
|
for (xn, yn) in neighbors:
|
||||||
@@ -31,7 +29,7 @@ def part_1(input):
|
|||||||
next_cost = cur_cost + nodes[n]
|
next_cost = cur_cost + nodes[n]
|
||||||
if not n in cost or next_cost < cost[n]:
|
if not n in cost or next_cost < cost[n]:
|
||||||
cost[n] = next_cost
|
cost[n] = next_cost
|
||||||
queue.add(n)
|
heappush(queue, (next_cost, n))
|
||||||
done.add(current)
|
done.add(current)
|
||||||
result = cost[end]
|
result = cost[end]
|
||||||
print("Part 1 result:", result)
|
print("Part 1 result:", result)
|
||||||
@@ -47,16 +45,13 @@ def part_2(input):
|
|||||||
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}
|
||||||
queue = set([start])
|
queue = [(0, start)]
|
||||||
done = set()
|
done = set()
|
||||||
neighbors = set([(-1, 0), (0, -1), (0, 1), (1, 0)])
|
neighbors = set([(-1, 0), (0, -1), (0, 1), (1, 0)])
|
||||||
while len(queue):
|
while len(queue):
|
||||||
cur_cost = None
|
cur_cost, current = heappop(queue)
|
||||||
for n in queue:
|
if current in done:
|
||||||
if cur_cost == None or cost[n] < cur_cost:
|
continue
|
||||||
cur_cost = cost[n]
|
|
||||||
current = n
|
|
||||||
queue.remove(current)
|
|
||||||
(xc, yc) = current
|
(xc, yc) = current
|
||||||
next = set()
|
next = set()
|
||||||
for (xn, yn) in neighbors:
|
for (xn, yn) in neighbors:
|
||||||
@@ -72,7 +67,7 @@ def part_2(input):
|
|||||||
next_cost -= 9
|
next_cost -= 9
|
||||||
if not (x, y) in cost or (cur_cost + next_cost) < cost[(x, y)]:
|
if not (x, y) in cost or (cur_cost + next_cost) < cost[(x, y)]:
|
||||||
cost[(x, y)] = cur_cost + next_cost
|
cost[(x, y)] = cur_cost + next_cost
|
||||||
queue.add((x, y))
|
heappush(queue, (cur_cost + next_cost, (x, y)))
|
||||||
done.add(current)
|
done.add(current)
|
||||||
result = cost[end]
|
result = cost[end]
|
||||||
print("Part 2 result:", result)
|
print("Part 2 result:", result)
|
||||||
|
|||||||
Reference in New Issue
Block a user