parent
0db48968c1
commit
1d916e940a
3 changed files with 69 additions and 0 deletions
@ -0,0 +1,49 @@ |
||||
import numpy as np |
||||
|
||||
|
||||
def readGrid(): |
||||
with open("input") as file: |
||||
lines = file.read().splitlines() |
||||
grid = np.array([[val for val in line] for line in lines], dtype=int) |
||||
return grid |
||||
|
||||
|
||||
def getAdjacent(grid, x, y): |
||||
h, w = grid.shape |
||||
adjAdd = [ |
||||
(-1, -1), |
||||
(-1, 0), |
||||
(-1, 1), |
||||
(0, 1), |
||||
(1, 1), |
||||
(1, 0), |
||||
(1, -1), |
||||
(0, -1) |
||||
] |
||||
adj = [(x + addX, y + addY) for addX, addY in adjAdd] |
||||
return [(x, y) for x, y in adj if w > x >= 0 and h > y >= 0] |
||||
|
||||
|
||||
def solve(grid): |
||||
flashCount = 0 |
||||
i = 0 |
||||
while True: |
||||
i += 1 |
||||
grid += 1 |
||||
allFlashPositions = set() |
||||
while len(grid[grid > 9]) > len(allFlashPositions): |
||||
flashPositions = {(x, y) for y, x in np.argwhere(grid > 9)}.difference(allFlashPositions) |
||||
for x, y in flashPositions: |
||||
for ax, ay in getAdjacent(grid, x, y): |
||||
grid[ay][ax] += 1 |
||||
allFlashPositions = allFlashPositions.union(flashPositions) |
||||
for x, y in allFlashPositions: |
||||
grid[y][x] = 0 |
||||
flashCount += len(allFlashPositions) if i <= 100 else 0 |
||||
if len(allFlashPositions) == 100: |
||||
break |
||||
|
||||
return flashCount, i |
||||
|
||||
|
||||
print(solve(readGrid())) |
@ -0,0 +1,10 @@ |
||||
4134384626 |
||||
7114585257 |
||||
1582536488 |
||||
4865715538 |
||||
5733423513 |
||||
8532144181 |
||||
1288614583 |
||||
2248711141 |
||||
6415871681 |
||||
7881531438 |
@ -0,0 +1,10 @@ |
||||
5483143223 |
||||
2745854711 |
||||
5264556173 |
||||
6141336146 |
||||
6357385478 |
||||
4167524645 |
||||
2176841721 |
||||
6882881134 |
||||
4846848554 |
||||
5283751526 |
Loading…
Reference in new issue