advent-of-code/2023/day13.py
Mikaël Capelle 8e90bf7002 2023 day 13.
2023-12-13 20:05:05 +01:00

44 lines
1.0 KiB
Python

import sys
from typing import Callable, Literal
def split(block: list[str], axis: Literal[0, 1], count: int) -> int:
n_iter = len(block) if axis == 0 else len(block[0])
n_check = len(block) if axis == 1 else len(block[0])
at: Callable[[int, int], str] = (
(lambda i, j: block[i][j]) if axis == 0 else (lambda i, j: block[j][i])
)
for i in range(n_iter - 1):
size = min(i + 1, n_iter - i - 1)
if (
sum(
at(i - s, j) != at(i + 1 + s, j)
for s in range(0, size)
for j in range(n_check)
)
== count
):
return i + 1
return 0
blocks = [block.splitlines() for block in sys.stdin.read().split("\n\n")]
# part 1
answer_1 = sum(
split(block, axis=1, count=0) + 100 * split(block, axis=0, count=0)
for block in blocks
)
print(f"answer 1 is {answer_1}")
# part 2
answer_2 = sum(
split(block, axis=1, count=1) + 100 * split(block, axis=0, count=1)
for block in blocks
)
print(f"answer 2 is {answer_2}")