feat: add solution 2025/04

This commit is contained in:
2025-12-04 06:41:03 +01:00
parent 00000080ac
commit 00000090e4
2 changed files with 68 additions and 0 deletions

10
data/example/2025/04.txt Normal file
View File

@@ -0,0 +1,10 @@
..@@.@@@@.
@@@.@.@.@@
@@@@@.@.@@
@.@@@@..@.
@@.@@@@.@@
.@@@@@@@.@
.@.@.@.@@@
@.@@@.@@@@
.@@@@@@@@.
@.@.@@@.@.

View File

@@ -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