-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
6617113584 | ||
6544218638 | ||
5457331488 | ||
1135675587 | ||
1221353216 | ||
1811124378 | ||
1387864368 | ||
4427637262 | ||
6778645486 | ||
3682146745 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
5483143223 | ||
2745854711 | ||
5264556173 | ||
6141336146 | ||
6357385478 | ||
4167524645 | ||
2176841721 | ||
6882881134 | ||
4846848554 | ||
5283751526 |