Skip to content

Commit c8cbfbb

Browse files
committed
Day 10
1 parent e80266a commit c8cbfbb

File tree

4 files changed

+128
-2
lines changed

4 files changed

+128
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ In accordance with the rules of this event, my input files (`input.txt`) are not
3737
| 7 |||
3838
| 8 |||
3939
| 9 |||
40-
| 10 | | |
40+
| 10 | | |
4141
| 11 |||
4242
| 12 |||
4343
| 13 |||
@@ -70,7 +70,7 @@ In accordance with the rules of this event, my input files (`input.txt`) are not
7070
| 7 | `itertools.product` to get all possible operator combinations | Only a 3-line difference |
7171
| 8 | `itertools.combinations` to get all possible _antenna_ pairs | |
7272
| 9 | | Heaps with `heapq` to keep a sorted list of free space indices by length |
73-
| 10 | | |
73+
| 10 | Recursion | Accidentally wrote the code for it while trying to do part 1 |
7474
| 11 | | |
7575
| 12 | | |
7676
| 13 | | |

day_10/part_1.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
def parse_input():
2+
INPUT = "input.txt"
3+
with open(INPUT) as file:
4+
arr = []
5+
t = []
6+
for i, line in enumerate(file.read().splitlines()):
7+
curr = [int(c) for c in line]
8+
arr.append(curr)
9+
for j, c in enumerate(curr):
10+
if c == 0:
11+
t.append((i, j))
12+
return arr, t
13+
14+
15+
def scan(
16+
topographic_map: list[list[int]],
17+
x: int,
18+
y: int,
19+
height: int,
20+
reached: set[tuple[int, int]],
21+
) -> None:
22+
if height == 9:
23+
reached.add((x, y))
24+
25+
bounds = []
26+
for z, a in [(x + 1, y), (x, y + 1), (x - 1, y), (x, y - 1)]:
27+
if (
28+
z >= 0
29+
and a >= 0
30+
and z < len(topographic_map)
31+
and a < len(topographic_map[0])
32+
and topographic_map[z][a] == height + 1
33+
):
34+
bounds.append((z, a))
35+
36+
if not len(bounds):
37+
return
38+
39+
for z, a in bounds:
40+
scan(topographic_map, z, a, height + 1, reached)
41+
42+
43+
def solve():
44+
topographic_map, trailheads = parse_input()
45+
sum_scores = 0
46+
for x, y in trailheads:
47+
reached: set[tuple[int, int]] = set()
48+
scan(topographic_map, x, y, 0, reached)
49+
sum_scores += len(reached)
50+
print(sum_scores)
51+
52+
53+
if __name__ == "__main__":
54+
import time
55+
56+
start_time = time.time()
57+
solve()
58+
print("--- %s seconds ---" % (time.time() - start_time))

day_10/part_2.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
def parse_input():
2+
INPUT = "input.txt"
3+
with open(INPUT) as file:
4+
arr = []
5+
t = []
6+
for i, line in enumerate(file.read().splitlines()):
7+
curr = [int(c) for c in line]
8+
arr.append(curr)
9+
for j, c in enumerate(curr):
10+
if c == 0:
11+
t.append((i, j))
12+
return arr, t
13+
14+
15+
def scan(topographic_map: list[list[int]], x: int, y: int, height: int) -> int:
16+
if height == 9:
17+
return 1
18+
19+
bounds = []
20+
for z, a in [(x + 1, y), (x, y + 1), (x - 1, y), (x, y - 1)]:
21+
if (
22+
z >= 0
23+
and a >= 0
24+
and z < len(topographic_map)
25+
and a < len(topographic_map[0])
26+
and topographic_map[z][a] == height + 1
27+
):
28+
bounds.append((z, a))
29+
30+
if not len(bounds):
31+
return 0
32+
33+
s = 0
34+
for z, a in bounds:
35+
s += scan(topographic_map, z, a, height + 1)
36+
return s
37+
38+
39+
def solve():
40+
topographic_map, trailheads = parse_input()
41+
sum_ratings = 0
42+
for x, y in trailheads:
43+
sum_ratings += scan(topographic_map, x, y, 0)
44+
print(sum_ratings)
45+
46+
47+
if __name__ == "__main__":
48+
import time
49+
50+
start_time = time.time()
51+
solve()
52+
print("--- %s seconds ---" % (time.time() - start_time))

template.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
def parse_input():
2+
INPUT = "input.txt"
3+
with open(INPUT) as file:
4+
pass
5+
6+
7+
def solve():
8+
pass
9+
10+
11+
if __name__ == "__main__":
12+
import time
13+
14+
start_time = time.time()
15+
solve()
16+
print("--- %s seconds ---" % (time.time() - start_time))

0 commit comments

Comments
 (0)