feat: add solution 2025/04
This commit is contained in:
10
data/example/2025/04.txt
Normal file
10
data/example/2025/04.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
..@@.@@@@.
|
||||
@@@.@.@.@@
|
||||
@@@@@.@.@@
|
||||
@.@@@@..@.
|
||||
@@.@@@@.@@
|
||||
.@@@@@@@.@
|
||||
.@.@.@.@@@
|
||||
@.@@@.@@@@
|
||||
.@@@@@@@@.
|
||||
@.@.@@@.@.
|
||||
58
src/solution/year_2025/day_04.py
Normal file
58
src/solution/year_2025/day_04.py
Normal 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
|
||||
Reference in New Issue
Block a user