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

+51
+51
2023/day13.py
··· 1 + import fileinput 2 + from utils import transposed 3 + 4 + 5 + def find_reflections(pattern): 6 + """Returns all possible reflections for pattern.""" 7 + sols = [] 8 + 9 + # First try solving as-is, then transpose for vertical reflections. 10 + for multiplier in [100, 1]: 11 + for y in range(len(pattern) - 1): 12 + bad = False 13 + for a, b in zip(reversed(pattern[:y+1]), pattern[y+1:]): 14 + if a != b: 15 + bad = True 16 + break 17 + 18 + if not bad: 19 + sols.append((y + 1) * multiplier) 20 + 21 + pattern = transposed(pattern) 22 + 23 + return sols 24 + 25 + 26 + def unsmudge(pattern, reflection): 27 + for y in range(len(pattern)): 28 + for x in range(len(pattern[0])): 29 + cell = pattern[y][x] 30 + pattern[y][x] = '.' if cell == '#' else '#' 31 + 32 + for new_reflection in find_reflections(pattern): 33 + if new_reflection != reflection: 34 + return new_reflection 35 + 36 + pattern[y][x] = cell 37 + 38 + 39 + part_1 = 0 40 + part_2 = 0 41 + 42 + for pattern in ''.join(fileinput.input()).split('\n\n'): 43 + pattern = [list(x) for x in pattern.splitlines()] 44 + 45 + reflection = solve(pattern)[0] 46 + part_1 += reflection 47 + part_2 += unsmudge(pattern, reflection) 48 + 49 + print("Part 1:", part_1) 50 + print("Part 2:", part_2) 51 +