-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday11.py
38 lines (33 loc) · 1.26 KB
/
day11.py
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
30
31
32
33
34
35
36
37
from utils import get_input
import numpy as np
def part1(data):
return count_flashes(data.copy(), 100)
def part2(data):
return count_flashes(data.copy(), 9999)
def count_flashes(data, steps):
f = 0
for step in range(steps):
flashed = []
for i in range(len(data)):
for j in range(len(data[0])):
data[i][j] += 1
while np.max(data)>9:
for i in range(len(data)):
for j in range(len(data[0])):
if data[i][j]>9 and (i,j) not in flashed:
data[i][j] = 0
f += 1
flashed.append((i,j))
for di, dj in [(-1,-1), (0,-1), (1,-1), (-1,0), (1,0), (-1,1), (0,1), (1,1)]:
if 0 <= i+di < len(data):
if 0 <= j+dj < len(data[i+di]):
if (i+di,j+dj) not in flashed:
data[i+di][j+dj] += 1
if len(flashed)==len(data)*len(data[0]): #reuse code for part 2
return(step+1)
return f
if __name__ == "__main__":
day = 11
data = np.array([[int(x) for x in line] for line in get_input(day)])
print(part1(data))
print(part2(data))