-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay 11.py
83 lines (76 loc) · 2.95 KB
/
Day 11.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
test = [[-1] + [int(x) for x in i] + [-1] for i in'''5483143223
2745854711
5264556173
6141336146
6357385478
4167524645
2176841721
6882881134
4846848554
5283751526'''.split('\n')]
test = [[-1 for i in range(len(test[0]))]] + test + [[-1 for i in range(len(test[0]))]]
inp = [[-1] + [int(x) for x in i] + [-1] for i in open('Day 11.txt','r').read().split('\n')]
inp = [[-1 for i in range(len(inp[0]))]] + inp + [[-1 for i in range(len(inp[0]))]]
def done(inp):
for y in range(1,len(inp)-1):
for x in range(1,len(inp[y])-1):
if inp[y][x] > 9:
return False
return True
def printgrid(inp):
string = ''
for y in range(1,len(inp)-1):
for x in range(1,len(inp[y])-1):
string += str(inp[y][x])
string += '\n'
print(string)
def part1(inp):
flashes = 0
for steps in range(100):
for y in range(1,len(inp)-1):
for x in range(1,len(inp[y])-1):
inp[y][x] += 1
while not done(inp):
for y in range(1,len(inp)-1):
for x in range(1,len(inp[y])-1):
if inp[y][x] > 9:
inp[y][x] = 0
flashes += 1
if inp[y+1][x+1] > 0: inp[y+1][x+1] += 1
if inp[y-1][x-1] > 0: inp[y-1][x-1] += 1
if inp[y+1][x-1] > 0: inp[y+1][x-1] += 1
if inp[y-1][x+1] > 0: inp[y-1][x+1] += 1
if inp[y+1][x] > 0: inp[y+1][x] += 1
if inp[y-1][x] > 0: inp[y-1][x] += 1
if inp[y][x+1] > 0: inp[y][x+1] += 1
if inp[y][x-1] > 0: inp[y][x-1] += 1
print(flashes)
def part2(inp):
step = 0
while True:
step += 1
flashes = 0
for y in range(1,len(inp)-1):
for x in range(1,len(inp[y])-1):
inp[y][x] += 1
while not done(inp):
for y in range(1,len(inp)-1):
for x in range(1,len(inp[y])-1):
if inp[y][x] > 9:
inp[y][x] = 0
flashes += 1
if inp[y+1][x+1] > 0: inp[y+1][x+1] += 1
if inp[y-1][x-1] > 0: inp[y-1][x-1] += 1
if inp[y+1][x-1] > 0: inp[y+1][x-1] += 1
if inp[y-1][x+1] > 0: inp[y-1][x+1] += 1
if inp[y+1][x] > 0: inp[y+1][x] += 1
if inp[y-1][x] > 0: inp[y-1][x] += 1
if inp[y][x+1] > 0: inp[y][x+1] += 1
if inp[y][x-1] > 0: inp[y][x-1] += 1
if flashes == 100:
break
print(step)
part1(inp)
inp = [[-1] + [int(x) for x in i] + [-1] for i in open('Day 11.txt','r').read().split('\n')]
inp = [[-1 for i in range(len(inp[0]))]] + inp + [[-1 for i in range(len(inp[0]))]]
part2(inp)