-
Notifications
You must be signed in to change notification settings - Fork 2
/
aoc202313.py
66 lines (53 loc) · 1.71 KB
/
aoc202313.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""AoC 13, 2023: Point of Incidence."""
# Standard library imports
import pathlib
import sys
def parse_data(puzzle_input):
"""Parse input."""
return [
{
(row, col)
for row, line in enumerate(block.split("\n"), start=1)
for col, char in enumerate(line, start=1)
if char == "#"
}
for block in puzzle_input.split("\n\n")
]
def part1(data):
"""Solve part 1."""
return sum(
find_mirror(grid) * 100 + find_mirror({(col, row) for row, col in grid})
for grid in data
)
def part2(data):
"""Solve part 2."""
return sum(
find_mirror(grid, 1) * 100 + find_mirror({(col, row) for row, col in grid}, 1)
for grid in data
)
def find_mirror(grid, num_smudges=0):
"""Find a row mirror."""
max_row = max(row for row, _ in grid)
for flip_row in range(1, max_row):
flipped = {(2 * flip_row + 1 - row, col) for row, col in grid}
not_mirrored = set(
range(1, flip_row - (max_row - flip_row) + 1)
if flip_row > max_row / 2
else range(2 * flip_row + 1, max_row + 1)
)
remaining = {
(row, col) for row, col in grid - flipped if row not in not_mirrored
}
if len(remaining) == num_smudges:
return flip_row
return 0
def solve(puzzle_input):
"""Solve the puzzle for the given input."""
data = parse_data(puzzle_input)
yield part1(data)
yield part2(data)
if __name__ == "__main__":
for path in sys.argv[1:]:
print(f"\n{path}:")
solutions = solve(puzzle_input=pathlib.Path(path).read_text().rstrip())
print("\n".join(str(solution) for solution in solutions))