day11
This commit is contained in:
parent
4a8b06b141
commit
65c31164f5
50
day11.py
Normal file
50
day11.py
Normal file
@ -0,0 +1,50 @@
|
||||
from tools.aoc import AOCDay
|
||||
from tools.coordinate import Coordinate
|
||||
from tools.grid import Grid
|
||||
from typing import Any, List
|
||||
|
||||
|
||||
def getOctopusGrid(lines: List[str]) -> Grid:
|
||||
grid = Grid(-1)
|
||||
for y, l in enumerate(lines):
|
||||
for x, c in enumerate(l):
|
||||
grid.set(Coordinate(x, y), int(c))
|
||||
|
||||
return grid
|
||||
|
||||
|
||||
def flashGrid(grid: Grid) -> int:
|
||||
flashers = set()
|
||||
for c in grid.getActiveCells():
|
||||
if grid.add(c) > 9:
|
||||
flashers.add(c)
|
||||
|
||||
did_flash = set()
|
||||
while len(flashers) > 0:
|
||||
thisFlash = flashers.pop()
|
||||
did_flash.add(thisFlash)
|
||||
grid.set(thisFlash, 0)
|
||||
|
||||
for n in grid.getNeighboursOf(thisFlash):
|
||||
if n not in did_flash and grid.add(n) > 9:
|
||||
flashers.add(n)
|
||||
|
||||
return len(did_flash)
|
||||
|
||||
|
||||
class Day(AOCDay):
|
||||
test_solutions_p1 = [1656]
|
||||
test_solutions_p2 = [195]
|
||||
|
||||
def part1(self) -> Any:
|
||||
grid = getOctopusGrid(self.getInput())
|
||||
return sum([flashGrid(grid) for _ in range(100)])
|
||||
|
||||
def part2(self) -> Any:
|
||||
grid = getOctopusGrid(self.getInput())
|
||||
step_count = 0
|
||||
while grid.getSum() != 0:
|
||||
step_count += 1
|
||||
flashGrid(grid)
|
||||
|
||||
return step_count
|
||||
10
inputs/input11
Normal file
10
inputs/input11
Normal file
@ -0,0 +1,10 @@
|
||||
8548335644
|
||||
6576521782
|
||||
1223677762
|
||||
1284713113
|
||||
6125654778
|
||||
6435726842
|
||||
5664175556
|
||||
1445736556
|
||||
2248473568
|
||||
6451473526
|
||||
10
inputs/test_input11_1_0
Normal file
10
inputs/test_input11_1_0
Normal file
@ -0,0 +1,10 @@
|
||||
5483143223
|
||||
2745854711
|
||||
5264556173
|
||||
6141336146
|
||||
6357385478
|
||||
4167524645
|
||||
2176841721
|
||||
6882881134
|
||||
4846848554
|
||||
5283751526
|
||||
10
inputs/test_input11_2_0
Normal file
10
inputs/test_input11_2_0
Normal file
@ -0,0 +1,10 @@
|
||||
5483143223
|
||||
2745854711
|
||||
5264556173
|
||||
6141336146
|
||||
6357385478
|
||||
4167524645
|
||||
2176841721
|
||||
6882881134
|
||||
4846848554
|
||||
5283751526
|
||||
Loading…
Reference in New Issue
Block a user