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/18

+49
+49
2024/day18.py
··· 1 + import fileinput 2 + from collections import deque 3 + 4 + from utils import Point, parse_nums 5 + 6 + 7 + def bfs(size, corruptions, time, start, end): 8 + horizon = deque([(start, 0)]) 9 + seen = set() 10 + while horizon: 11 + pos, dist = horizon.popleft() 12 + if pos in seen: 13 + continue 14 + 15 + seen.add(pos) 16 + 17 + if pos == END: 18 + return dist 19 + 20 + for n in pos.neighbours(): 21 + is_corrupted = corruptions.get(n, 1e6) 22 + if is_corrupted < time: 23 + continue 24 + 25 + if 0 <= n.x <= size and 0 <= n.y <= size: 26 + horizon.append((n, dist + 1)) 27 + 28 + 29 + SIZE = 70 30 + 31 + # Read problem input. 32 + BYTES = [] 33 + CORRUPTIONS = {} 34 + for i, line in enumerate(fileinput.input()): 35 + BYTES.append(line) 36 + x, y = parse_nums(line) 37 + CORRUPTIONS[Point(x, y)] = i 38 + 39 + START = Point(0, 0) 40 + END = Point(SIZE, SIZE) 41 + 42 + # Solve problem. 43 + for i in range(len(BYTES)): 44 + dist = bfs(SIZE, CORRUPTIONS, i, Point(0, 0), Point(SIZE, SIZE)) 45 + if i == 1024: 46 + print("Part 1:", dist) 47 + if dist is None: 48 + print("Part 2:", BYTES[i-1]) 49 + break