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 2022/12

+40
+40
2022/day12.py
··· 1 + import fileinput 2 + from collections import deque 3 + from utils import Point 4 + 5 + # Parse input. 6 + board = {} 7 + for y, line in enumerate(fileinput.input()): 8 + for x, c in enumerate(line.strip()): 9 + board[Point(x, y)] = c 10 + if c == 'S': 11 + start = Point(x, y) 12 + board[start] = 'a' 13 + elif c == 'E': 14 + end = Point(x, y) 15 + board[end] = 'z' 16 + 17 + 18 + def bfs(start): 19 + horizon = deque([(start, 0)]) 20 + seen = set() 21 + while horizon: 22 + p, depth = horizon.popleft() 23 + if p in seen: 24 + continue 25 + elif p == end: 26 + return depth 27 + 28 + seen.add(p) 29 + 30 + for n in p.neighbours(): # returns the 4 adjacent points 31 + if n not in board: 32 + continue 33 + if (ord(board[n]) - 1 <= ord(board[p])): 34 + horizon.append((n, depth + 1)) 35 + 36 + return 1e9 37 + 38 + 39 + print("Part 1:", bfs(start)) 40 + print("Part 2:", min(bfs(s) for s in board if board[s] == 'a'))