-
Notifications
You must be signed in to change notification settings - Fork 0
/
day10.py
48 lines (44 loc) · 1.56 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
from typing import Set, List, Tuple, Dict
with open("input.txt") as input_file:
input_lines = input_file.readlines()
input_lines = [line.strip('\n') for line in input_lines]
Row = int
Col = int
Height = int
Position = Tuple[Row, Col]
grid: List[List[Height]] = []
for line in input_lines:
grid.append([int(c) for c in line])
g_height = len(grid)
g_width = len(grid[0])
answer_1 = 0
answer_2 = 0
for row in range(g_height):
for col in range(g_width):
trailhead = grid[row][col]
if trailhead == 0:
score = 0
rating = 0
visit_counters: Dict[Position, int] = { (row, col): 1 }
queue = [(row, col)]
while queue:
r, c = queue.pop(0)
if grid[r][c] == 9:
score += 1
rating += visit_counters[(r, c)]
continue
for dr, dc in ((+1, 0), (-1, 0), (0, +1), (0, -1)):
nr, nc = r + dr, c + dc
if not (0 <= nr < g_height and 0 <= nc < g_width):
continue
if grid[nr][nc] != grid[r][c] + 1:
continue
if (nr, nc) in visit_counters:
visit_counters[(nr, nc)] += visit_counters[(r, c)]
continue
visit_counters[(nr, nc)] = visit_counters[(r, c)]
queue.append((nr, nc))
answer_1 += score
answer_2 += rating
print("Answer 1:", answer_1) # 430
print("Answer 2:", answer_2) # 928