82 lines
1.6 KiB
Python
82 lines
1.6 KiB
Python
class Boi(object):
|
|
def __init__(self, num):
|
|
self.num = num
|
|
self.flashed = False
|
|
|
|
def __repr__(self):
|
|
return str(self.num)
|
|
|
|
|
|
bois = {}
|
|
|
|
# with open("input.txt") as f:
|
|
with open("sample.txt") as f:
|
|
y = 0
|
|
for line in f.readlines():
|
|
x = 0
|
|
for c in line.strip():
|
|
bois[(x, y, )] = Boi(int(c))
|
|
x += 1
|
|
y += 1
|
|
|
|
|
|
def neighbors(point):
|
|
l = []
|
|
for dx in range(-1, 2):
|
|
for dy in range(-1, 2):
|
|
p = (point[0] + dx, point[1] + dy, )
|
|
if p == point:
|
|
continue
|
|
try:
|
|
l.append(bois[p])
|
|
except KeyError:
|
|
pass
|
|
return l
|
|
|
|
|
|
def simulate():
|
|
flashes = 0
|
|
# import pdb
|
|
# pdb.set_trace()
|
|
# perform 1 step of simulation
|
|
|
|
# incr all bois by 1
|
|
for boi in bois.values():
|
|
boi.num += 1
|
|
|
|
all_hit = set()
|
|
|
|
while True:
|
|
hit = []
|
|
# flash all bois > 9
|
|
for point, boi in bois.items():
|
|
if boi.num > 9 and not boi.flashed:
|
|
flashes += 1
|
|
boi.num = 0
|
|
boi.flashed = True
|
|
all_hit.update([point])
|
|
hit.extend(neighbors(point))
|
|
|
|
# increment all bois that are hit by the flash
|
|
for neighbor in hit:
|
|
neighbor.num += 1
|
|
|
|
if not hit:
|
|
break
|
|
|
|
# reset flashes
|
|
for boi in bois.values():
|
|
if boi.flashed:
|
|
boi.num = 0
|
|
boi.flashed = False
|
|
|
|
return len(all_hit)
|
|
|
|
|
|
for wave in range(0, 10000):
|
|
if simulate() == 100:
|
|
print(wave)
|
|
break
|
|
|
|
# 43m
|