···
file = '4.input' if len(sys.argv) <= 1 else sys.argv[1]
-
grid = list(map(str.strip, open(file).readlines()))
-
def check_occupied(y: int, x: int) -> int:
-
return x >= 0 and y >= 0 and grid[y][x] == "@"
-
for y in range(len(grid)):
-
for x in range(len(row)):
-
if row[x] != "@": continue
-
check_occupied(y-1, x-1), check_occupied(y-1, x), check_occupied(y-1, x+1),
-
check_occupied(y, x-1), check_occupied(y, x+1),
-
check_occupied(y+1, x-1), check_occupied(y+1, x), check_occupied(y+1, x+1)
-
print(f'p1: {available_rolls}')
···
+
from typing import List, Optional, Tuple
file = '4.input' if len(sys.argv) <= 1 else sys.argv[1]
+
grid = list(map(list, map(str.strip, open(file).readlines())))
+
def check_occupied(y: int, x: int) -> Optional[Tuple[int, int]]:
+
if x >= 0 and y >= 0 and grid[y][x] == "@":
+
except IndexError: pass
+
def compute_adj(y: int, x: int) -> List[Optional[Tuple[int, int]]]:
+
check_occupied(y-1, x-1), check_occupied(y-1, x), check_occupied(y-1, x+1),
+
check_occupied(y, x-1), check_occupied(y, x+1),
+
check_occupied(y+1, x-1), check_occupied(y+1, x), check_occupied(y+1, x+1)
+
def search_removable() -> List[Tuple[int, int]]:
+
for y in range(len(grid)):
+
for x in range(len(grid[y])):
+
if grid[y][x] != "@": continue
+
adj = compute_adj(y, x)
+
if sum(map(bool, adj)) < 4:
+
removable.append((y, x))
+
removable = search_removable()
+
print(f'p1: {len(removable)}')
+
for (y, x) in removable:
+
removable = search_removable()
+
print(f'p2: {removed_rolls}')