-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathday10.py
executable file
·70 lines (51 loc) · 1.15 KB
/
day10.py
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
#!/usr/bin/env python3
from utils.all import *
advent.setup(2024, 10)
fin = advent.get_input()
grid2 = read_digit_matrix(fin)
ans1 = ans2 = 0
def dfs_count_9(grid, r, c):
q = deque([(r, c)])
seen = set()
total = 0
while q:
node = q.pop()
if node in seen:
continue
seen.add(node)
r, c = node
if grid[r][c] == 9:
total += 1
val = grid[r][c]
for rr, cc in neighbors4(grid, r, c):
if grid[rr][cc] == val + 1:
q.append((rr, cc))
return total
def dfs_count_path_to_9(grid, r, c):
q = deque([((r, c), ((r, c),))])
seen = set()
paths = set()
while q:
node = q.pop()
if node in seen:
continue
seen.add(node)
(r, c), path = node
if grid[r][c] == 9:
paths.add(path)
val = grid[r][c]
for rr, cc in neighbors4(grid, r, c):
if grid[rr][cc] == val + 1:
q.append(((rr, cc), path + ((rr, cc))))
return len(paths)
for r, row in enumerate(grid2):
for c, x in enumerate(row):
if x == 0:
ans1 += dfs_count_9(grid2, r, c)
# 469 wrong
advent.print_answer(1, ans1)
for r, row in enumerate(grid2):
for c, x in enumerate(row):
if x == 0:
ans2 += dfs_count_path_to_9(grid2, r, c)
advent.print_answer(2, ans2)