-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday04.py
More file actions
29 lines (24 loc) · 789 Bytes
/
Copy pathday04.py
File metadata and controls
29 lines (24 loc) · 789 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import itertools
grid = open("inputs/day04.txt").read().split("\n")
def movable(y, x):
count = 0
for dy, dx in itertools.product([-1, 0, 1], [-1, 0, 1]):
ny, nx = y + dy, x + dx
if 0 <= ny < len(grid) and 0 <= nx < len(grid[0]) and grid[ny][nx] == "@":
count += 1
return count < 5
res_a = 0
for y, line in enumerate(grid):
for x, c in enumerate(line):
if c == "@":
res_a += int(movable(y, x))
print(f"a = {res_a}")
changed = True
while changed:
changed = False
for y, line in enumerate(grid):
for x, c in enumerate(line):
if c == "@" and movable(y, x):
grid[y] = line[:x] + "-" + line[x+1:]
changed = True
print(f"b = {sum(line.count("-") for line in grid)}")