tangled
alpha
login
or
join now
kot.pink
/
aoc
2
fork
atom
advent of code 2025
2
fork
atom
overview
issues
pulls
pipelines
day 4 part 2
kot.pink
3 months ago
04e3f385
f50d2e4c
verified
This commit was signed with the committer's
known signature
.
kot.pink
SSH Key Fingerprint:
SHA256:e4bjV3/Jkmz9iPYLTjvwj9VNZdgdgRm8GIc0LYqo0kU=
+34
-19
1 changed file
expand all
collapse all
unified
split
4.py
+34
-19
4.py
···
1
1
import sys
2
2
+
from typing import List, Optional, Tuple
2
3
3
3
-
available_rolls = 0
4
4
+
removed_rolls = 0
4
5
file = '4.input' if len(sys.argv) <= 1 else sys.argv[1]
5
5
-
grid = list(map(str.strip, open(file).readlines()))
6
6
+
grid = list(map(list, map(str.strip, open(file).readlines())))
6
7
7
7
-
def check_occupied(y: int, x: int) -> int:
8
8
+
def check_occupied(y: int, x: int) -> Optional[Tuple[int, int]]:
8
9
try:
9
9
-
return x >= 0 and y >= 0 and grid[y][x] == "@"
10
10
-
except IndexError:
11
11
-
return False
10
10
+
if x >= 0 and y >= 0 and grid[y][x] == "@":
11
11
+
return (y, x)
12
12
+
except IndexError: pass
13
13
+
return None
14
14
+
15
15
+
def compute_adj(y: int, x: int) -> List[Optional[Tuple[int, int]]]:
16
16
+
return [
17
17
+
check_occupied(y-1, x-1), check_occupied(y-1, x), check_occupied(y-1, x+1),
18
18
+
check_occupied(y, x-1), check_occupied(y, x+1),
19
19
+
check_occupied(y+1, x-1), check_occupied(y+1, x), check_occupied(y+1, x+1)
20
20
+
]
21
21
+
22
22
+
def search_removable() -> List[Tuple[int, int]]:
23
23
+
removable = []
24
24
+
for y in range(len(grid)):
25
25
+
for x in range(len(grid[y])):
26
26
+
if grid[y][x] != "@": continue
27
27
+
adj = compute_adj(y, x)
28
28
+
if sum(map(bool, adj)) < 4:
29
29
+
removable.append((y, x))
30
30
+
return removable
12
31
13
13
-
for y in range(len(grid)):
14
14
-
row = grid[y]
15
15
-
for x in range(len(row)):
16
16
-
if row[x] != "@": continue
17
17
-
adj = sum([
18
18
-
check_occupied(y-1, x-1), check_occupied(y-1, x), check_occupied(y-1, x+1),
19
19
-
check_occupied(y, x-1), check_occupied(y, x+1),
20
20
-
check_occupied(y+1, x-1), check_occupied(y+1, x), check_occupied(y+1, x+1)
21
21
-
])
22
22
-
if adj < 4:
23
23
-
available_rolls += 1
32
32
+
removable = search_removable()
33
33
+
print(f'p1: {len(removable)}')
34
34
+
35
35
+
while len(removable):
36
36
+
for (y, x) in removable:
37
37
+
grid[y][x] = "."
38
38
+
removed_rolls += 1
39
39
+
removable = search_removable()
24
40
25
25
-
print(f'p1: {available_rolls}')
26
26
-
# print(f'p2: {}')
41
41
+
print(f'p2: {removed_rolls}')