-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday18.py
More file actions
72 lines (54 loc) · 1.71 KB
/
day18.py
File metadata and controls
72 lines (54 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from lib.consts import DIR_CROSS
from lib.input import get_input
from lib.map import Position
WIDTH = 71
HEIGHT = 71
lines = get_input(2024, 18).splitlines()
bugs = [Position(*tuple(map(int, line.split(",")))) for line in lines]
class Node:
def __init__(self, value, pos: Position):
self.value = value
self.pos = pos
self.neighbours: list[Node] = []
nodes = []
nodes_by_coords: dict[Position, Node] = {}
for y in range(HEIGHT):
for x in range(WIDTH):
pos = Position(x, y)
node = Node(1, pos)
for dir in DIR_CROSS:
neigh = nodes_by_coords.get(pos + dir)
if neigh is None:
continue
node.neighbours.append(neigh)
neigh.neighbours.append(node)
nodes.append(node)
nodes_by_coords[pos] = node
start = nodes_by_coords.get(Position(0, 0))
stop = nodes_by_coords.get(Position(WIDTH - 1, HEIGHT - 1))
def delete_node(coord: Position):
node = nodes_by_coords.get(coord)
for neigh in node.neighbours:
neigh.neighbours.remove(node)
nodes_by_coords.pop(coord)
nodes.remove(node)
def dijkstra(start):
dist = {node: float("inf") for node in nodes}
dist[start] = 0
queue = [start]
while queue:
node = queue.pop(0)
for neigh in node.neighbours:
if dist[neigh] > dist[node] + neigh.value:
dist[neigh] = dist[node] + neigh.value
queue.append(neigh)
return dist
def part1():
for bug in bugs[:1024]:
delete_node(bug)
return dijkstra(start)[stop]
def part2():
for bug in bugs[1024:]:
delete_node(bug)
if dijkstra(start)[stop] == float("inf"):
return f"{bug.x},{bug.y}"