day 11
This commit is contained in:
parent
eb760604be
commit
4e7c3a6569
78
11/a.py
Normal file
78
11/a.py
Normal file
@ -0,0 +1,78 @@
|
||||
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:
|
||||
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
|
||||
|
||||
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
|
||||
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 flashes
|
||||
|
||||
|
||||
s = 0
|
||||
for wave in range(0, 100):
|
||||
f = simulate()
|
||||
print("wave", wave, "flashes", f)
|
||||
s += f
|
||||
|
||||
print(s)
|
81
11/b.py
Normal file
81
11/b.py
Normal file
@ -0,0 +1,81 @@
|
||||
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
|
10
11/input.txt
Normal file
10
11/input.txt
Normal file
@ -0,0 +1,10 @@
|
||||
8448854321
|
||||
4447645251
|
||||
6542573645
|
||||
4725275268
|
||||
6442514153
|
||||
4515734868
|
||||
5513676158
|
||||
3257376185
|
||||
2172424467
|
||||
6775163586
|
10
11/sample.txt
Normal file
10
11/sample.txt
Normal file
@ -0,0 +1,10 @@
|
||||
5483143223
|
||||
2745854711
|
||||
5264556173
|
||||
6141336146
|
||||
6357385478
|
||||
4167524645
|
||||
2176841721
|
||||
6882881134
|
||||
4846848554
|
||||
5283751526
|
Loading…
Reference in New Issue
Block a user