From db802844bd4ea488e98804b98c78aef97d23e1b4 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Fri, 5 Dec 2025 08:45:01 +0000 Subject: [PATCH] 2025: d04: ex2: add solution --- 2025/d04/ex2/ex2.py | 47 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100755 2025/d04/ex2/ex2.py diff --git a/2025/d04/ex2/ex2.py b/2025/d04/ex2/ex2.py new file mode 100755 index 0000000..d5d1f4a --- /dev/null +++ b/2025/d04/ex2/ex2.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python + +import itertools +import sys +from typing import NamedTuple + + +class Point(NamedTuple): + x: int + y: int + + +def solve(input: list[str]) -> int: + def parse(input: list[str]) -> set[Point]: + return { + Point(x, y) + for x, line in enumerate(input) + for y, c in enumerate(line) + if c == "@" + } + + def is_accessible(point: Point, rolls: set[Point]) -> bool: + assert point in rolls # Sanity check + return ( + sum( + Point(point.x + dx, point.y + dy) in rolls + for dx, dy in itertools.product(range(-1, 1 + 1), repeat=2) + if dx != 0 or dy != 0 + ) + < 4 + ) + + rolls = parse(input) + removed = 0 + while accessible := {p for p in rolls if is_accessible(p, rolls)}: + removed += len(accessible) + rolls -= accessible + return removed + + +def main() -> None: + input = sys.stdin.read().splitlines() + print(solve(input)) + + +if __name__ == "__main__": + main()