diff options
-rw-r--r-- | 13.py | 35 |
1 files changed, 35 insertions, 0 deletions
@@ -0,0 +1,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") |