Skip to content

Commit 1a53da8

Browse files
committed
Add day 04, 2025
1 parent 5abadd0 commit 1a53da8

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

2025/day04/solution.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
with open("input") as f:
2+
inp = f.read().strip().split("\n")
3+
4+
grid = {x + y*1j: c for y, line in enumerate(inp) for x, c in enumerate(line)}
5+
6+
7+
def accessible_rolls(grid):
8+
dirs = [1, -1, 1j, -1j, 1+1j, 1-1j, -1+1j, -1-1j]
9+
res = []
10+
for p in grid:
11+
if grid[p] == ".":
12+
continue
13+
rolls = 0
14+
for dp in dirs:
15+
if p + dp in grid and grid[p + dp] == "@":
16+
rolls += 1
17+
if rolls < 4:
18+
res.append(p)
19+
20+
return res
21+
22+
23+
# Part 1
24+
print(len(accessible_rolls(grid)))
25+
26+
27+
# Part 2
28+
def update_grid(grid):
29+
ps = accessible_rolls(grid)
30+
for p in ps:
31+
grid[p] = "."
32+
33+
return len(ps), grid
34+
35+
36+
ans = 0
37+
while True:
38+
n, grid = update_grid(grid)
39+
ans += n
40+
if n == 0:
41+
break
42+
43+
print(ans)

0 commit comments

Comments
 (0)