-
Notifications
You must be signed in to change notification settings - Fork 0
/
10.py
61 lines (49 loc) · 1.36 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
56
57
58
59
60
61
# pylint: skip-file
# mypy: ignore-errors
# flake8: noqa
from collections import defaultdict
from functools import cache
input_value = open("10.txt", "r").read()
lines = input_value.split("\n")
rows = len(lines)
columns = len(lines[0])
grid = defaultdict(lambda: None)
for r in range(rows):
for c in range(columns):
grid[r, c] = int(lines[r][c])
# Part 1:
total = 0
for r in range(rows):
for c in range(columns):
if grid[r, c] != 0:
continue
count = 0
seen = set()
stack = [(r, c)]
while stack:
(r1, c1) = stack.pop()
seen.add((r1, c1))
if grid[r1, c1] == 9:
count += 1
continue
for neighbor in {(r1 - 1, c1), (r1 + 1, c1), (r1, c1 - 1), (r1, c1 + 1)}:
if neighbor not in seen and grid[neighbor] == grid[r1, c1] + 1:
stack.append(neighbor)
total += count
print(total)
@cache
def ways_from(r, c):
if grid[r, c] == 9:
return 1
total = 0
for neighbor in {(r - 1, c), (r + 1, c), (r, c - 1), (r, c + 1)}:
if grid[neighbor] == grid[r, c] + 1:
total += ways_from(neighbor[0], neighbor[1])
return total
# Part 2:
ways = 0
for r in range(rows):
for c in range(columns):
if grid[r, c] == 0:
ways += ways_from(r, c)
print(ways)