-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution11-2.py
More file actions
87 lines (63 loc) · 1.85 KB
/
solution11-2.py
File metadata and controls
87 lines (63 loc) · 1.85 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
def valid(r, c):
return r >= 0 and r < 10 and c >= 0 and c < 10
def incrementgrid(g):
for x in range(0, 10):
for y in range(0, 10):
g[x][y] += 1
def process_flashes(g, d, r, c):
flag = False
for dir in d:
new_r = r + dir[0]
new_c = c + dir[1]
if valid(new_r, new_c) and g[new_r][new_c] < 10 and g[new_r][new_c] >= 0:
g[new_r][new_c] += 1
if g[new_r][new_c] == 10:
flag = True
return flag
def process_input(g, directions):
answer = 0
flag = False
for r in range(0, 10):
for c in range(0, 10):
if g[r][c] == 10:
answer += 1
g[r][c] = -1
for r in range(0, 10):
for c in range(0, 10):
if g[r][c] == -1:
if process_flashes(g, directions, r, c):
flag = True
g[r][c] = -2
# check for new flashes
if flag:
return answer + process_input(g, directions)
return answer
def main():
file = open("input11.txt", "r")
file = file.readlines()
input = []
answer = 0
directions = [[1, 1], [-1, -1], [1, 0], [0, 1], [-1, 0], [0, -1], [1, -1], [-1, 1]]
# parse input
for line in file:
row = line.split("\n")[0]
row = list(row)
row = [int(x) for x in row]
input.append(row)
for x in range(1, 5000):
cleancount = 0
# increment one
incrementgrid(input)
# process results
answer += process_input(input, directions)
# cleanie boy
for r in range(0, 10):
for c in range(0, 10):
if input[r][c] == -2:
cleancount += 1
input[r][c] = 0
if cleancount == 100:
print("the answer is: ", str(x))
break
print(answer)
main()