File tree Expand file tree Collapse file tree 2 files changed +51
-1
lines changed Expand file tree Collapse file tree 2 files changed +51
-1
lines changed Original file line number Diff line number Diff line change
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 ()
Original file line number Diff line number Diff line change 1
1
# Advent of Code
2
2
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 )
4
4
5
5
[ My] ( https://github.com/tobiasvl ) solutions for the puzzles of [ Advent of Code] ( http://adventofcode.com ) .
6
6
You can’t perform that action at this time.
0 commit comments