-
Notifications
You must be signed in to change notification settings - Fork 0
/
11-B.py
66 lines (53 loc) · 1.85 KB
/
11-B.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
56
57
58
59
60
61
62
63
64
65
66
class octopi:
def __init__(self, board):
self.board = board
self.counter= 0
self.flashed =[]
def rise(self):
for i in range(len(self.board)):
for j in range(len(self.board[i])):
self.board[i][j] += 1
def flash_all(self):
for i in range(len(self.board)):
for j in range(len(self.board[i])):
if self.board[i][j] > 9 and (i,j) not in self.flashed:
self.flashed.append((i,j))
self.flash(i, j)
def reset(self):
for i in range(len(self.board)):
for j in range(len(self.board[i])):
if self.board[i][j] > 9:
self.board[i][j] = 0
self.flashed.clear()
def flash(self, x,y):
for i in range(x-1, x+2):
for j in range(y-1, y+2):
if (i < 0 or j < 0) or (x==i and y==j) or i > len(self.board)-1 or j > len(self.board)-1:
continue # in case we would go out of bounds, also ignore self.
else:
self.board[i][j] += 1
if self.board[i][j] > 9 and (i,j) not in self.flashed: #can only flash once.
self.flashed.append((i,j))
self.flash(i,j)
def check_for_simul_flash(self):
arr = []
for line in self.board:
arr.extend(line)
if len(set(arr)) == 1: # each char only once in a set.
return True
return False
with open("11-input.txt") as file:
lines = [x.strip() for x in file.readlines()]
board = [list(map(int, x)) for x in lines]
sum = 0
oct = octopi(board)
#TIL: python does not have a do while loop
i = 0
while True:
i += 1
oct.rise()
oct.flash_all()
oct.reset()
if oct.check_for_simul_flash():
print(i)
break