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

+39
+39
2024/day04.py
··· 1 + import fileinput 2 + from utils import Point, DIRS_8, NW, NE, SW, SE 3 + 4 + BOARD = {} 5 + X_LOCS = set() 6 + A_LOCS = set() 7 + 8 + for y, line in enumerate(fileinput.input()): 9 + for x, c in enumerate(line.strip()): 10 + p = Point(x, y) 11 + BOARD[p] = c 12 + if c == 'X': 13 + X_LOCS.add(p) 14 + elif c == 'A': 15 + A_LOCS.add(p) 16 + 17 + 18 + # Solve part 1. 19 + part_1 = 0 20 + for loc in X_LOCS: 21 + for d in DIRS_8: 22 + if (BOARD.get(loc + d) == 'M' 23 + and BOARD.get(loc + d * 2) == 'A' 24 + and BOARD.get(loc + d * 3) == 'S'): 25 + part_1 += 1 26 + 27 + print("Part 1:", part_1) 28 + 29 + 30 + # Solve part 2. 31 + part_2 = 0 32 + for loc in A_LOCS: 33 + diag_1 = set([BOARD.get(loc + NW), BOARD.get(loc + SE)]) 34 + diag_2 = set([BOARD.get(loc + NE), BOARD.get(loc + SW)]) 35 + 36 + if diag_1 == diag_2 == set(['M', 'S']): 37 + part_2 += 1 38 + 39 + print("Part 2:", part_2)