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 2025/06

+58
+58
2025/day06.py
··· 1 + import fileinput 2 + import math 3 + import re 4 + 5 + GROUP_RE = r'[\d*+]+' 6 + 7 + 8 + # Parse problem input. 9 + MATRIX = [] 10 + ALL_NUMS = [] 11 + for y, line in enumerate(fileinput.input()): 12 + # Parse whole groups for part 1. 13 + groups = re.findall(GROUP_RE, line) 14 + if '*' not in groups: 15 + ALL_NUMS.append(groups) 16 + else: 17 + OPS = groups 18 + 19 + # Add a "simulated" blank line before the list of 20 + # operations to make part 2 processing easier. 21 + MATRIX.append([' ' for _ in range(len(MATRIX[-1]))]) 22 + 23 + # Create a matrix to transpose for part 2. 24 + MATRIX.append(list(line)) 25 + 26 + 27 + # Solve part 1. 28 + OPERANDS = list(zip(*ALL_NUMS)) 29 + PART_1 = 0 30 + for operands, op in zip(OPERANDS, OPS): 31 + operands = [int(n) for n in operands] 32 + if op == '+': 33 + PART_1 += sum(operands) 34 + else: 35 + PART_1 += math.prod(operands) 36 + 37 + print("Part 1:", PART_1) 38 + 39 + 40 + # Solve part 2. 41 + FLIPPED = list(zip(*MATRIX)) 42 + TAPE = ''.join(''.join(col) for col in reversed(FLIPPED)) 43 + PART_2 = 0 44 + 45 + buffer = [] 46 + for group in re.findall(GROUP_RE, TAPE): 47 + if group == '+': 48 + PART_2 += sum(buffer) 49 + buffer = [] 50 + elif group == '*': 51 + PART_2 += math.prod(buffer) 52 + buffer = [] 53 + else: 54 + buffer.append(int(group)) 55 + 56 + print("Part 2:", PART_2) 57 + 58 +