-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp11a.py
56 lines (46 loc) · 1016 Bytes
/
p11a.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# https://adventofcode.com/2021/day/11 part 1
# Created by: Menaka S. 11 Dec 2021
import sys
lines = []
def split(word):
return [int(char) for char in word]
def increment(i,j):
if i >= 0 and i < len(lines[0]) and j >= 0 and j < len(lines[0]):
lines[i][j] += 1
if lines[i][j] > 9 and (i,j) not in flashed:
toflash.add((i,j))
return 1
return 0
def flash(i,j):
#print("Flashing..",i,j)
flashed.add((i,j))
increment(i+1,j-1)
increment(i+1,j)
increment(i+1,j+1)
increment(i,j-1)
increment(i,j+1)
increment(i-1,j-1)
increment(i-1,j)
increment(i-1,j+1)
for line in sys.stdin:
lines.append(split(line.strip()))
step = 0
mx = 100
flashes = 0
while step < mx:
toflash = set()
flashed = set()
for i in range(len(lines[0])):
for j in range(len(lines[0])):
lines[i][j] +=1
if(lines[i][j] > 9):
toflash.add((i,j))
while len(toflash) > 0:
(i,j) = toflash.pop()
flash(i,j)
for (x,y) in flashed:
if lines[x][y] > 9:
lines[x][y] = 0
flashes += 1
step +=1
print(flashes)