Skip to content

Commit 6b6de86

Browse files
committed
Day 10 Python solutions
1 parent 77d4cc0 commit 6b6de86

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed

10-1.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/env python
2+
3+
import numpy
4+
5+
6+
class Map:
7+
def __init__(self, input):
8+
self.height = len(input)
9+
self.width = len(input[0])
10+
self.trailheads = []
11+
self.peaks = []
12+
self.map = numpy.zeros((self.height, self.width), dtype=numpy.uint8)
13+
for y in range(self.height):
14+
for x in range(self.width):
15+
match input[y][x]:
16+
case '.':
17+
self.map[y, x] = 255
18+
case '0':
19+
self.map[y, x] = 0
20+
self.trailheads.append((y, x))
21+
case '9':
22+
self.map[y, x] = 9
23+
self.peaks.append((y, x))
24+
case _:
25+
self.map[y, x] = int(input[y][x])
26+
27+
def trace(self, y, x, visited):
28+
if visited[y, x] == 1:
29+
return 0
30+
31+
visited[y, x] = 1
32+
cur_height = self.map[y, x]
33+
34+
if cur_height == 9:
35+
return 1
36+
37+
score = 0
38+
if y + 1 < self.height and self.map[y + 1, x] == cur_height + 1:
39+
score += self.trace(y + 1, x, visited)
40+
if y - 1 >= 0 and self.map[y - 1, x] == cur_height + 1:
41+
score += self.trace(y - 1, x, visited)
42+
if x + 1 < self.width and self.map[y, x + 1] == cur_height + 1:
43+
score += self.trace(y, x + 1, visited)
44+
if x - 1 >= 0 and self.map[y, x - 1] == cur_height + 1:
45+
score += self.trace(y, x - 1, visited)
46+
return score
47+
48+
def hike(self):
49+
score = 0
50+
for t in self.trailheads:
51+
score += self.trace(t[0], t[1],
52+
numpy.zeros((self.height, self.width), dtype=numpy.uint8))
53+
return score
54+
55+
56+
map = Map([row.strip() for row in open('10.input').readlines()])
57+
print(map.hike())

10-2.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/usr/bin/env python
2+
3+
import numpy
4+
5+
6+
class Map:
7+
def __init__(self, input):
8+
self.height = len(input)
9+
self.width = len(input[0])
10+
self.trailheads = []
11+
self.peaks = []
12+
self.map = numpy.zeros((self.height, self.width), dtype=numpy.uint8)
13+
for y in range(self.height):
14+
for x in range(self.width):
15+
match input[y][x]:
16+
case '.':
17+
self.map[y, x] = 255
18+
case '0':
19+
self.map[y, x] = 0
20+
self.trailheads.append((y, x))
21+
case '9':
22+
self.map[y, x] = 9
23+
self.peaks.append((y, x))
24+
case _:
25+
self.map[y, x] = int(input[y][x])
26+
27+
def trace(self, y, x, visited):
28+
if (y, x) in visited:
29+
return 0
30+
31+
cur_height = self.map[y, x]
32+
33+
if cur_height == 9:
34+
return 1
35+
36+
score = 0
37+
if y + 1 < self.height and self.map[y + 1, x] == cur_height + 1:
38+
score += self.trace(y + 1, x, visited + [(y, x)])
39+
if y - 1 >= 0 and self.map[y - 1, x] == cur_height + 1:
40+
score += self.trace(y - 1, x, visited + [(y, x)])
41+
if x + 1 < self.width and self.map[y, x + 1] == cur_height + 1:
42+
score += self.trace(y, x + 1, visited + [(y, x)])
43+
if x - 1 >= 0 and self.map[y, x - 1] == cur_height + 1:
44+
score += self.trace(y, x - 1, visited + [(y, x)])
45+
return score
46+
47+
def hike(self):
48+
score = 0
49+
for t in self.trailheads:
50+
score += self.trace(t[0], t[1], [])
51+
return score
52+
53+
54+
map = Map([row.strip() for row in open('10.input').readlines()])
55+
print(map.hike())

0 commit comments

Comments
 (0)