diff --git a/data/example/2025/04.txt b/data/example/2025/04.txt new file mode 100644 index 000000000..8209399 --- /dev/null +++ b/data/example/2025/04.txt @@ -0,0 +1,10 @@ +..@@.@@@@. +@@@.@.@.@@ +@@@@@.@.@@ +@.@@@@..@. +@@.@@@@.@@ +.@@@@@@@.@ +.@.@.@.@@@ +@.@@@.@@@@ +.@@@@@@@@. +@.@.@@@.@. diff --git a/src/solution/year_2025/day_04.py b/src/solution/year_2025/day_04.py new file mode 100644 index 000000000..02f18d2 --- /dev/null +++ b/src/solution/year_2025/day_04.py @@ -0,0 +1,58 @@ +from typing import Any + +dirs = [(0, 1), (0, -1), (1, 0), (-1, 0), (1, 1), (-1, -1), (-1, 1), (1, -1)] + + +def _count_and_remove_rolls(diagram: list[list[str]], replace: bool = False) -> int: + c = 0 + for i, line in enumerate(diagram): + for j, cell in enumerate(line): + if cell != "@": + continue + + n = 0 + for di, dj in dirs: + ni, nj = (i + di, j + dj) + + if ( + (0 <= ni < len(diagram)) + and (0 <= nj < len(line)) + and diagram[ni][nj] == "@" + ): + n += 1 + + if n >= 4: + continue + + c += 1 + if replace: + diagram[i][j] = "." + + return c + + +def part_1(input_data: str) -> Any: + diagram = [list(x) for x in input_data.splitlines()] + return _count_and_remove_rolls(diagram) + + +def part_2(input_data: str) -> Any: + diagram = [list(x) for x in input_data.splitlines()] + + c = 0 + while True: + n = _count_and_remove_rolls(diagram, True) + if n == 0: + break + + c += n + + return c + + +def test_part_1(example_data): + assert part_1(example_data) == 13 + + +def test_part_2(example_data): + assert part_2(example_data) == 43