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

+42
+42
2025/day02.py
··· 1 + import fileinput 2 + from utils import parts 3 + 4 + 5 + # Read problem input. 6 + RANGES = [] 7 + for line in fileinput.input(): 8 + for r in line.split(','): 9 + x, y = r.split('-') 10 + RANGES.append((int(x), int(y))) 11 + 12 + 13 + # Solve puzzle. 14 + PART_1 = 0 15 + PART_2 = 0 16 + 17 + for x, y in RANGES: 18 + part_1_invalids = set() 19 + part_2_invalids = set() 20 + 21 + for n in range(x, y + 1): 22 + invalid = False 23 + for d in range(2, len(str(n)) + 1): 24 + n = str(n) 25 + if len(n) % d != 0: 26 + continue 27 + 28 + # Split the string `n` into `d` parts and 29 + # check if they're all the same string. 30 + if len(set(parts(n, d))) == 1: 31 + if d == 2: 32 + part_1_invalids.add(int(n)) 33 + 34 + part_2_invalids.add(int(n)) 35 + break 36 + 37 + PART_1 += sum(part_1_invalids) 38 + PART_2 += sum(part_2_invalids) 39 + 40 + 41 + print("Part 1:", PART_1) 42 + print("Part 2:", PART_2)