You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.3 KiB
51 lines
1.3 KiB
#!/usr/bin/env python3
|
|
|
|
import sys
|
|
|
|
def flash(octop, x, y):
|
|
for i in [(-1,-1), (-1,0), (-1,1), (0,-1), (0,1), (1,-1), (1,0), (1,1)]:
|
|
if -1 < y+i[0] < len(octop) and -1 < x+i[1] < len(octop):
|
|
if octop[y+i[0]][x+i[1]] > 0: octop[y+i[0]][x+i[1]] += 1
|
|
octop[y][x] = 0
|
|
return octop
|
|
|
|
def flashable(octo):
|
|
for y in octo:
|
|
for x in y:
|
|
if x > 9:
|
|
return True
|
|
return False
|
|
|
|
def step(octo):
|
|
nex = [[x+1 for x in y] for y in octo]
|
|
flashs = 0
|
|
while flashable(nex):
|
|
for x in range(len(octo[0])):
|
|
for y in range(len(octo)):
|
|
if nex[y][x] > 9:
|
|
nex = flash(nex,x,y)
|
|
flashs += 1
|
|
return nex, flashs
|
|
|
|
if __name__ == '__main__':
|
|
lines = open(sys.argv[1]).read().strip('\n\n').split('\n') # or split \n\n or sth similar
|
|
octos = [[int(i) for i in l] for l in lines]
|
|
|
|
# challenge 1
|
|
flashes, i = 0, 0
|
|
while True:
|
|
i += 1
|
|
octos, f = step(octos)
|
|
if(i <= 100):
|
|
flashes += f
|
|
if sum([sum([1 for x in range(len(octos[0])) if octos[y][x] == 0]) for y in range(len(octos))]) == 100:
|
|
first = i
|
|
break
|
|
|
|
res1 = str(flashes)
|
|
print("challenge 1:" +"\n" + res1 + "\n")
|
|
|
|
# challenge 2
|
|
res2 = str(first)
|
|
print("challenge 2:" +"\n" + res2 + "\n")
|
|
|
|
|