Skip to content

Commit 32bff4e

Browse files
committed
feat: 2025 day04
1 parent 81fcd63 commit 32bff4e

7 files changed

Lines changed: 131 additions & 27 deletions

File tree

.pre-commit-config.yaml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
repos:
22
- repo: https://github.com/astral-sh/ruff-pre-commit
3-
rev: v0.14.7
3+
rev: v0.14.8
44
hooks:
5-
- id: ruff
6-
args: [--fix]
5+
- id: ruff-check
76
- id: ruff-format

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
| 01 | [][012015] | | | | [][012019] | [][012020] | [][012021] | [][012022] | [][012023] | [][012024] | [][012025] |
88
| 02 | [][022015] | | | | [][022019] | [][022020] | [][022021] | [][022022] | [][022023] | [][022024] | [][022025] |
99
| 03 | [][032015] | | | | [][032019] | [][032020] | [][032021] | [][032022] | [][032023] | [][032024] | [][032025] |
10-
| 04 | [][042015] | | | | [][042019] | [][042020] | [][042021] | [][042022] | [][042023] | [][042024] | |
10+
| 04 | [][042015] | | | | [][042019] | [][042020] | [][042021] | [][042022] | [][042023] | [][042024] | [][042025] |
1111
| 05 | [][052015] | | | | | [][052020] | | [][052022] | [][052023] | [][052024] | [][052025] |
1212
| 06 | [][062015] | | | | | [][062020] | | [][062022] | [][062023] | [][062024] | |
1313
| 07 | [][072015] | | | | | [][072020] | | [][072022] | [][072023] | [][072024] | |
@@ -132,7 +132,7 @@
132132
[012025]: https://github.com/fabiogallotti/adventofcode/tree/master/src/year2025/day01
133133
[022025]: https://github.com/fabiogallotti/adventofcode/tree/master/src/year2025/day02
134134
[032025]: https://github.com/fabiogallotti/adventofcode/tree/master/src/year2025/day03
135-
135+
[042025]: https://github.com/fabiogallotti/adventofcode/tree/master/src/year2025/day04
136136
[052025]: https://github.com/fabiogallotti/adventofcode/tree/master/src/year2025/day05
137137

138138
## To run an exercise ##

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ description = ""
1414
readme = "README.md"
1515

1616
[dependency-groups]
17-
dev = ["pre-commit>=4.5.0", "pytest>=9.0.1", "ruff>=0.14.7"]
17+
dev = ["pre-commit>=4.5.0", "pytest>=9.0.1", "ruff>=0.14.8"]
1818

1919
[build-system]
2020
requires = ["pdm-backend"]

src/year2025/day04/exercises.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from functions.read_input import read_input
2+
from inputs.path import PATH
3+
4+
from .functions import part_1, part_2
5+
6+
data = read_input(f"{PATH}/2025/day04.txt")
7+
8+
print(f"First part: {part_1(data)}")
9+
10+
print(f"Second part: {part_2(data)}")

src/year2025/day04/functions.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
from enum import Enum
2+
3+
from pydantic import BaseModel
4+
5+
6+
class Position(BaseModel):
7+
x: int
8+
y: int
9+
10+
def __hash__(self):
11+
return hash(str(self.x) + str(self.y))
12+
13+
14+
class Direction(Enum):
15+
UP = (-1, 0)
16+
RIGHT = (0, 1)
17+
DOWN = (1, 0)
18+
LEFT = (0, -1)
19+
UP_RIGHT = (-1, 1)
20+
DOWN_RIGHT = (1, 1)
21+
DOWN_LEFT = (1, -1)
22+
UP_LEFT = (-1, -1)
23+
24+
25+
def preprocessing(data):
26+
paper = set()
27+
for i in range(len(data)):
28+
for j in range(len(data[0])):
29+
elem = data[i][j]
30+
if elem == "@":
31+
paper.add(Position(x=i, y=j))
32+
33+
return paper
34+
35+
36+
def part_1(data):
37+
directions = list(Direction)
38+
paper = preprocessing(data)
39+
total = 0
40+
41+
for elem in paper:
42+
count = 0
43+
for direction in directions:
44+
dx, dy = direction.value
45+
next_pos = Position(x=elem.x + dx, y=elem.y + dy)
46+
if next_pos in paper:
47+
count += 1
48+
if count < 4:
49+
total += 1
50+
return total
51+
52+
53+
def part_2(data):
54+
directions = list(Direction)
55+
paper = preprocessing(data)
56+
total = 0
57+
58+
while True:
59+
to_remove = set()
60+
for elem in paper:
61+
count = 0
62+
for direction in directions:
63+
dx, dy = direction.value
64+
next_pos = Position(x=elem.x + dx, y=elem.y + dy)
65+
if next_pos in paper:
66+
count += 1
67+
if count < 4:
68+
total += 1
69+
to_remove.add(elem)
70+
if not to_remove:
71+
break
72+
paper -= to_remove
73+
return total

tests/2025/test_2025day04.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from year2025.day04.functions import part_1, part_2
2+
3+
EXAMPLE_1 = [
4+
"..@@.@@@@.",
5+
"@@@.@.@.@@",
6+
"@@@@@.@.@@",
7+
"@.@@@@..@.",
8+
"@@.@@@@.@@",
9+
".@@@@@@@.@",
10+
".@.@.@.@@@",
11+
"@.@@@.@@@@",
12+
".@@@@@@@@.",
13+
"@.@.@@@.@.",
14+
]
15+
16+
17+
def test_part_1():
18+
assert part_1(EXAMPLE_1) == 13
19+
20+
21+
def test_part_2():
22+
assert part_2(EXAMPLE_1) == 43

uv.lock

Lines changed: 21 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)