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 2023/10 part 1

+85
+85
2023/day10.py
··· 1 + import fileinput 2 + from utils import Point, DIRS, N, E, S, W 3 + 4 + 5 + def transition(node, tile, dirr): 6 + if tile == '|': 7 + if dirr == N: 8 + return N, 1 9 + elif dirr == S: 10 + return S, 1 11 + else: 12 + return None, 0 13 + elif tile == '-': 14 + if dirr == W: 15 + return W, 1 16 + elif dirr == E: 17 + return E, 1 18 + else: 19 + return None, 0 20 + elif tile == 'L': 21 + if dirr == W: 22 + return S, 1 23 + elif dirr == N: 24 + return E, 1 25 + elif tile == 'J': 26 + if dirr == N: 27 + return W, 1 28 + elif dirr == E: 29 + return S, 1 30 + else: 31 + return None, 1 32 + elif tile == '7': 33 + if dirr == E: 34 + return N, 1 35 + elif dirr == S: 36 + return W, 1 37 + else: 38 + return None, 0 39 + elif tile == 'F': 40 + if dirr == S: 41 + return E, 1 42 + elif dirr == W: 43 + return N, 1 44 + else: 45 + return None, 0 46 + 47 + elif tile == 'S': 48 + return direction, 1 49 + 50 + return None, 0 51 + 52 + 53 + def walk_loop(graph, start, direction): 54 + loop = [start] 55 + node = start 56 + while True: 57 + direction, good = transition(node, graph[node], direction) 58 + if not good: 59 + return None 60 + 61 + node += direction 62 + loop.append(node) 63 + 64 + if node == start: 65 + return loop 66 + 67 + 68 + 69 + board = {} 70 + for y, line in enumerate(fileinput.input()): 71 + for x, c in enumerate(line.strip()): 72 + board[Point(x, y)] = c 73 + 74 + 75 + # Solve part 1. 76 + start = [p for p, v in board.items() if v == 'S'][0] 77 + 78 + for direction in DIRS: 79 + loop = walk_loop(board, start, direction) 80 + if loop: 81 + print("Part 1:", len(loop) // 2) 82 + break 83 + 84 + 85 + # TODO: Solve part 2 programmatically.