-
Notifications
You must be signed in to change notification settings - Fork 0
/
10.py
55 lines (39 loc) · 1.24 KB
/
10.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
from aoc.utilities.fetch import get_input
from aoc.utilities.decorators import solution
@solution
def solve_all(matrix):
n, m = len(matrix), len(matrix[0])
ans1, ans2 = 0, 0
for i in range(n):
for j in range(m):
if matrix[i][j] != 0:
continue
ans1 += len(dfs_first(i, j, matrix))
ans2 += dfs_second(i, j, matrix)
print(f"{ans1}\n{ans2}")
def dfs_first(i, j, matrix):
if matrix[i][j] == 9:
return set([(i, j)])
ans = set()
for ai, aj in [(i + 1, j), (i - 1, j), (i, j + 1), (i, j - 1)]:
if (
0 <= ai < len(matrix)
and 0 <= aj < len(matrix[0])
and matrix[ai][aj] == matrix[i][j] + 1
):
ans |= dfs_first(ai, aj, matrix)
return ans
def dfs_second(i, j, matrix):
if matrix[i][j] == 9:
return 1
ans = 0
for ai, aj in [(i + 1, j), (i - 1, j), (i, j + 1), (i, j - 1)]:
if (
0 <= ai < len(matrix)
and 0 <= aj < len(matrix[0])
and matrix[ai][aj] == matrix[i][j] + 1
):
ans += dfs_second(ai, aj, matrix)
return ans
matrix = [list(map(int, line)) for line in map(list, get_input(10).splitlines())]
solve_all(matrix)