Skip to content

Commit 88e0cf8

Browse files
committed
AoC 2024: Day 10
1 parent f8949d8 commit 88e0cf8

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

2024/day10/day10.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/python3
2+
3+
# Override lists and strings so negative indices don't wrap around
4+
class gridlist(list):
5+
def __getitem__(self, n):
6+
if n < 0:
7+
raise IndexError("Negative index")
8+
return list.__getitem__(self, n)
9+
10+
transforms = [
11+
(0, -1),
12+
(1, 0),
13+
(0, 1),
14+
(-1, 0)
15+
]
16+
17+
def adjacent(location):
18+
return [(location[0] + direction[0], location[1] + direction[1]) for direction in transforms]
19+
20+
def main():
21+
with open('input') as f:
22+
grid = gridlist([gridlist([int(a) for a in line.strip()]) for line in f.readlines()])
23+
24+
trailheads = list()
25+
for y in range(len(grid)):
26+
for x in range(len(grid[y])):
27+
if grid[y][x] == 0:
28+
trailheads.append((y,x))
29+
30+
def find(location, seen=None):
31+
if grid[location[0]][location[1]] == 9:
32+
if seen != None:
33+
if location in seen:
34+
return 0
35+
seen.add(location)
36+
return 1
37+
score = 0
38+
for new_location in adjacent(location):
39+
try:
40+
if grid[new_location[0]][new_location[1]] == grid[location[0]][location[1]] + 1:
41+
score += find(new_location, seen)
42+
except IndexError:
43+
pass
44+
return score
45+
46+
print(sum([find(location, set()) for location in trailheads]))
47+
print(sum([find(location) for location in trailheads]))
48+
49+
if __name__ == "__main__":
50+
main()

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Advent of Code
22

3-
![](https://img.shields.io/badge/2024%20⭐-9-yellow) ![](https://img.shields.io/badge/2022%20⭐-6-yellow) ![](https://img.shields.io/badge/2021%20⭐-23-yellow) ![](https://img.shields.io/badge/2020%20⭐-16-yellow) ![](https://img.shields.io/badge/2019%20⭐-1-yellow) ![](https://img.shields.io/badge/2018%20⭐-41-yellow) ![](https://img.shields.io/badge/2017%20⭐-50-yellow) ![](https://img.shields.io/badge/2016%20⭐-42-yellow) ![](https://img.shields.io/badge/2015%20⭐-12-yellow)
3+
![](https://img.shields.io/badge/2024%20⭐-10-yellow) ![](https://img.shields.io/badge/2022%20⭐-6-yellow) ![](https://img.shields.io/badge/2021%20⭐-23-yellow) ![](https://img.shields.io/badge/2020%20⭐-16-yellow) ![](https://img.shields.io/badge/2019%20⭐-1-yellow) ![](https://img.shields.io/badge/2018%20⭐-41-yellow) ![](https://img.shields.io/badge/2017%20⭐-50-yellow) ![](https://img.shields.io/badge/2016%20⭐-42-yellow) ![](https://img.shields.io/badge/2015%20⭐-12-yellow)
44

55
[My](https://github.com/tobiasvl) solutions for the puzzles of [Advent of Code](http://adventofcode.com).
66

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy