-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday10.py
67 lines (48 loc) · 1.53 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
INPUT = 'input'
def solve():
rows = [[int(char) for char in line] for line in read().splitlines()]
trailheads = find_trailheads(rows)
map = Map(rows)
ans1, ans2 = 0, 0
for head in trailheads:
paths = find_paths(map, head)
ans1 += len({path[-1] for path in paths})
ans2 += len(paths)
print(f'Part 1: {ans1}')
print(f'Part 2: {ans2}')
def find_paths(map, head):
paths = [[head]]
for height in range(1, 10):
paths = [
new_path
for path in paths
for new_path in extend_path(map, path, height)]
return paths
def extend_path(map, path, height):
for pos, val in map.adjacent(*path[-1]):
if val == height:
yield path + [pos]
def find_trailheads(rows):
return [
(x, y)
for y, row in enumerate(rows)
for x, char in enumerate(row)
if char == 0]
def read():
return open(f'./input/2024/day10/{INPUT}.txt').read().strip()
class Map:
adjacent_directions = [(0, -1), (1, 0), (0, 1), (-1, 0)]
def __init__(self, rows):
self.rows = rows
self.height = len(rows)
self.width = len(rows[0])
def value(self, x, y):
if y >= 0 and y < self.height and x >= 0 and x < self.width:
return self.rows[y][x]
def adjacent(self, x, y):
return [
((adj_x, adj_y), value)
for dx, dy in self.adjacent_directions
if (value := self.value((adj_x := x + dx), (adj_y := y + dy)))
is not None]
solve()