-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday10.py
More file actions
executable file
·64 lines (47 loc) · 1.5 KB
/
Copy pathday10.py
File metadata and controls
executable file
·64 lines (47 loc) · 1.5 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
from collections import deque
from aoc_utils import * # type: ignore
from aocd import get_data
data = get_data(year=2024, day=10, block=True)
def part_one(data):
grid = Grid.parse(data, cast=int)
graph = Graph()
head_candidates = set()
tail_candidates = set()
for yx, h1 in grid.row_major_with_index():
if h1 == 0:
head_candidates.add(yx)
elif h1 == 9:
tail_candidates.add(yx)
for ayx, h2 in grid.around_with_index(yx, corners=False):
if h2 == h1 + 1:
graph.add_edge(yx, ayx)
score = 0
for head in head_candidates:
dijkstra = graph.dijkstra(head)
for tail in tail_candidates:
points = dijkstra.path_to(tail)
if points:
score += 1
return score
def part_two(data):
grid = Grid.parse(data, cast=int)
graph = Graph()
head_candidates = set()
tail_ratings = 0
for yx, h1 in grid.row_major_with_index():
if h1 == 0:
head_candidates.add(yx)
for ayx, h2 in grid.around_with_index(yx, corners=False):
if h2 == h1 + 1:
graph.add_edge(yx, ayx)
for head in head_candidates:
queue = deque([head])
while queue:
node = queue.pop()
if grid[node] == 9:
tail_ratings += 1
for other_node in graph.edges(node):
queue.appendleft(other_node)
return tail_ratings
print(part_one(data))
print(part_two(data))