-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10-1.py
executable file
·57 lines (47 loc) · 1.79 KB
/
10-1.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
#!/usr/bin/env python
import numpy
class Map:
def __init__(self, input):
self.height = len(input)
self.width = len(input[0])
self.trailheads = []
self.peaks = []
self.map = numpy.zeros((self.height, self.width), dtype=numpy.uint8)
for y in range(self.height):
for x in range(self.width):
match input[y][x]:
case '.':
self.map[y, x] = 255
case '0':
self.map[y, x] = 0
self.trailheads.append((y, x))
case '9':
self.map[y, x] = 9
self.peaks.append((y, x))
case _:
self.map[y, x] = int(input[y][x])
def trace(self, y, x, visited):
if visited[y, x] == 1:
return 0
visited[y, x] = 1
cur_height = self.map[y, x]
if cur_height == 9:
return 1
score = 0
if y + 1 < self.height and self.map[y + 1, x] == cur_height + 1:
score += self.trace(y + 1, x, visited)
if y - 1 >= 0 and self.map[y - 1, x] == cur_height + 1:
score += self.trace(y - 1, x, visited)
if x + 1 < self.width and self.map[y, x + 1] == cur_height + 1:
score += self.trace(y, x + 1, visited)
if x - 1 >= 0 and self.map[y, x - 1] == cur_height + 1:
score += self.trace(y, x - 1, visited)
return score
def hike(self):
score = 0
for t in self.trailheads:
score += self.trace(t[0], t[1],
numpy.zeros((self.height, self.width), dtype=numpy.uint8))
return score
map = Map([row.strip() for row in open('10.input').readlines()])
print(map.hike())