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
|
# pyright: strict
from collections.abc import Iterable
from functools import cache
from sys import stdin
@cache
def rocks_to_int(bits: Iterable[str]) -> int:
return sum(2**i for i, bit in enumerate(bits) if bit == "#")
@cache
def diff(nums: tuple[int], cut: int) -> int:
return sum(
(nums[a] ^ nums[b]).bit_count()
for a, b in zip(range(cut - 1, -1, -1), range(cut, len(nums)))
)
def find_mirror(nums: tuple[int, ...], difference: int) -> int:
for n in range(1, len(nums)):
if diff(nums, n) == difference:
return n
return 0
p1 = p2 = 0
for pattern in stdin.read().rstrip("\n").split("\n\n"):
pattern = pattern.split("\n")
rows = tuple(map(rocks_to_int, pattern))
cols = tuple(map(rocks_to_int, zip(*pattern)))
p1 += find_mirror(cols, 0) or find_mirror(rows, 0) * 100
p2 += find_mirror(cols, 1) or find_mirror(rows, 1) * 100
print(p1, p2, sep="\n")
|