Skip to content

Commit

Permalink
day11
Browse files Browse the repository at this point in the history
  • Loading branch information
Zehvogel committed Dec 11, 2021
1 parent 3ef7d2a commit aba466a
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
10 changes: 10 additions & 0 deletions day11/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
6617113584
6544218638
5457331488
1135675587
1221353216
1811124378
1387864368
4427637262
6778645486
3682146745
75 changes: 75 additions & 0 deletions day11/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import time
import numpy as np

def incnflash(i, j, m, flashes):
m[i, j] += 1
if m[i, j] > 9 and not flashes[i, j]:
flashes[i, j] = True
flash(i, j, m, flashes)


def flash(i, j, m, flashes):
up = i > 0
left = j > 0
down = i < m.shape[0] - 1
right = j < m.shape[1] - 1
if up:
incnflash(i-1, j, m, flashes)
if left:
incnflash(i-1, j-1, m, flashes)
if right:
incnflash(i-1, j+1, m, flashes)
if down:
incnflash(i+1, j, m, flashes)
if left:
incnflash(i+1, j-1, m, flashes)
if right:
incnflash(i+1, j+1, m, flashes)
if left:
incnflash(i, j-1, m, flashes)
if right:
incnflash(i, j+1, m, flashes)

def do_step(m):
m += 1
flashes = m > 9
for i, j in np.argwhere(flashes):
flash(i, j, m, flashes)
m[flashes] = 0
return np.sum(flashes)

def calc_steps(m, steps):
res = 0
for i in range(steps):
res += do_step(m)
return res

def calc_steps2(m):
res = 0
flashes = 0
while flashes < m.size:
flashes = do_step(m)
res += 1
return res

def part1(input):
m = np.asarray([[int(x) for x in line.strip()] for line in input])
return calc_steps(m, 100)

def part2(input):
m = np.asarray([[int(x) for x in line.strip()] for line in input])
return calc_steps2(m)

def main():
with open("day11/input.txt") as f:
input = f.readlines()
t0 = time.perf_counter_ns()
print(f"solution for part 1 is: {part1(input)}")
t1 = time.perf_counter_ns()
print(f"part 1 took {t1-t0} ns")
print(f"solution for part 2 is: {part2(input)}")
t2 = time.perf_counter_ns()
print(f"part 2 took {t2-t1} ns")

if __name__ == "__main__":
main()
10 changes: 10 additions & 0 deletions day11/test_input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
5483143223
2745854711
5264556173
6141336146
6357385478
4167524645
2176841721
6882881134
4846848554
5283751526

0 comments on commit aba466a

Please sign in to comment.