My Advent of Code solutions in Python. kevinyap.ca/2019/12/going-fast-in-advent-of-code/
advent-of-code python
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add solution for 2024/10

+51
+51
2024/day10.py
··· 1 + import fileinput 2 + from collections import deque 3 + 4 + from utils import Point 5 + 6 + 7 + def hike(board, trailhead): 8 + horizon = deque([trailhead]) 9 + seen = set() 10 + 11 + nines = set() 12 + trails = 0 13 + 14 + while horizon: 15 + curr = horizon.pop() 16 + if curr in seen: 17 + continue 18 + 19 + if board.get(curr) == 9: 20 + trails += 1 21 + nines.add(curr) 22 + 23 + for neigh in curr.neighbours(): 24 + if board.get(neigh, 0) == board.get(curr) + 1: 25 + horizon.appendleft(neigh) 26 + 27 + return len(nines), trails 28 + 29 + 30 + # Read problem input. 31 + BOARD = {} 32 + TRAILHEADS = set() 33 + 34 + for y, line in enumerate(fileinput.input()): 35 + for x, c in enumerate(line.strip()): 36 + p = Point(x, y) 37 + BOARD[p] = int(c) 38 + if BOARD[p] == 0: 39 + TRAILHEADS.add(p) 40 + 41 + 42 + # Solve problem. 43 + part_1 = 0 44 + part_2 = 0 45 + for trailhead in TRAILHEADS: 46 + scores, trails = hike(BOARD, trailhead) 47 + part_1 += scores 48 + part_2 += trails 49 + 50 + print("Part 1:", part_1) 51 + print("Part 2:", part_2)