aoc

Advent of Code Solutions
git clone git://git.alexkarle.com.com/aoc
Log | Files | Refs | README | LICENSE

commit c3251b507fca55b469bc89cee97b406c26b38148 (patch)
parent a9fb336ca4774ae96bcdbfd2c9e52619df76e172
Author: Alex Karle <alex@alexkarle.com>
Date:   Sat, 11 Dec 2021 12:36:20 -0500

day11: Add python solution

Who knows if it's optimal? We're effectively doing a DFS using the
callstack on flashing neighbors...

Diffstat:
A11/a.py | 77+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A11/b.py | 79+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A11/example | 10++++++++++
A11/input | 10++++++++++
4 files changed, 176 insertions(+), 0 deletions(-)

diff --git a/11/a.py b/11/a.py @@ -0,0 +1,77 @@ +#!/usr/bin/env python3 +import sys + +G = [[0] * 10 for i in range(10)] +flashes = 0 + + +def debug(i=0): + print(f"--- {i} ---") + for r in G: + print("".join([str(i) for i in r])) + + +def step(): + flashed = [[False] * 10 for i in range(10)] + + def flash(r, c): + global flashes + + # At minimum, receive += 1 + G[r][c] += 1 + + # Now flash neighbors + if G[r][c] > 9 and not flashed[r][c]: + flashed[r][c] = True + flashes += 1 + # Neighbors + if r > 0: + flash(r - 1, c) + if c > 0: + flash(r, c - 1) + if r < 9: + flash(r + 1, c) + if c < 9: + flash(r, c + 1) + # corners + if r > 0 and c > 0: + flash(r - 1, c - 1) + if r < 9 and c < 9: + flash(r + 1, c + 1) + if r > 0 and c < 9: + flash(r - 1, c + 1) + if r < 9 and c > 0: + flash(r + 1, c - 1) + + # increase everyone first + for r in range(10): + for c in range(10): + G[r][c] += 1 + + # now flash it up + for r in range(10): + for c in range(10): + if G[r][c] > 9: + flash(r, c) + + # now, reset + for r in range(10): + for c in range(10): + if G[r][c] > 9: + G[r][c] = 0 + + +# Read in the grid +r = 0 +for l in sys.stdin: + c = 0 + for n in l.strip(): + G[r][c] = int(n) + c += 1 + r += 1 + +for i in range(100): + # debug(i) + step() + +print(flashes) diff --git a/11/b.py b/11/b.py @@ -0,0 +1,79 @@ +#!/usr/bin/env python3 +import sys + +G = [[0] * 10 for i in range(10)] + + +def debug(i=0): + print(f"--- {i} ---") + for r in G: + print("".join([str(i) for i in r])) + + +def step(): + flashed = [[False] * 10 for i in range(10)] + + def flash(r, c): + # At minimum, receive += 1 + G[r][c] += 1 + + # Now flash neighbors + if G[r][c] > 9 and not flashed[r][c]: + flashed[r][c] = True + # Neighbors + if r > 0: + flash(r - 1, c) + if c > 0: + flash(r, c - 1) + if r < 9: + flash(r + 1, c) + if c < 9: + flash(r, c + 1) + # corners + if r > 0 and c > 0: + flash(r - 1, c - 1) + if r < 9 and c < 9: + flash(r + 1, c + 1) + if r > 0 and c < 9: + flash(r - 1, c + 1) + if r < 9 and c > 0: + flash(r + 1, c - 1) + + # increase everyone first + for r in range(10): + for c in range(10): + G[r][c] += 1 + + # now flash it up + for r in range(10): + for c in range(10): + if G[r][c] > 9: + flash(r, c) + + # now, reset + flashes = 0 + for r in range(10): + for c in range(10): + if G[r][c] > 9: + G[r][c] = 0 + flashes += 1 + + # Return ! is sync + return flashes != 100 + + +# Read in the grid +r = 0 +for l in sys.stdin: + c = 0 + for n in l.strip(): + G[r][c] = int(n) + c += 1 + r += 1 + +i = 1 +while step(): + i += 1 + + +print(i) diff --git a/11/example b/11/example @@ -0,0 +1,10 @@ +5483143223 +2745854711 +5264556173 +6141336146 +6357385478 +4167524645 +2176841721 +6882881134 +4846848554 +5283751526 diff --git a/11/input b/11/input @@ -0,0 +1,10 @@ +5651341452 +1381541252 +1878435224 +6814831535 +3883547383 +6473548464 +1885833658 +3732584752 +1881546128 +5121717776