From c3251b507fca55b469bc89cee97b406c26b38148 Mon Sep 17 00:00:00 2001 From: Alex Karle Date: Sat, 11 Dec 2021 12:36:20 -0500 Subject: [PATCH] day11: Add python solution Who knows if it's optimal? We're effectively doing a DFS using the callstack on flashing neighbors... --- 11/a.py | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 11/b.py | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 11/example | 10 ++++++++++ 11/input | 10 ++++++++++ 4 files changed, 176 insertions(+) create mode 100755 11/a.py create mode 100755 11/b.py create mode 100644 11/example create mode 100644 11/input diff --git a/11/a.py b/11/a.py new file mode 100755 index 0000000..e304503 --- /dev/null +++ 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 new file mode 100755 index 0000000..0e02478 --- /dev/null +++ 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 new file mode 100644 index 0000000..03743f6 --- /dev/null +++ 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 new file mode 100644 index 0000000..efb0e67 --- /dev/null +++ b/11/input @@ -0,0 +1,10 @@ +5651341452 +1381541252 +1878435224 +6814831535 +3883547383 +6473548464 +1885833658 +3732584752 +1881546128 +5121717776 -- libgit2 1.8.1