~noelle/aoc-2021

f98d1969e0c00fc8702115179e97e067e4dc9fb7 — Noelle Leigh 2 years ago 349b296
11_2
1 files changed, 96 insertions(+), 0 deletions(-)

A 11/puzzle_2.py
A 11/puzzle_2.py => 11/puzzle_2.py +96 -0
@@ 0,0 1,96 @@
"""
Solution for AoC 2021 11 Puzzle 2

cat input.txt | python puzzle_2.py
"""
import sys

if __name__ == "__main__":
    octopi = list(
        map(
            lambda line: list(map(int, line)),
            sys.stdin.read().splitlines(),
        )
    )
    WIDTH = len(octopi[0])
    HEIGHT = len(octopi)
    simul_flash = 0
    for step in range(1, 1000 + 1):
        # Increment energy levels
        octopi = list(
            map(lambda row: list(map(lambda energy: energy + 1, row)), octopi)
        )
        # Look for flashes
        found_flashes = True
        while found_flashes:
            found_flashes = False
            for row_index in range(HEIGHT):
                for col_index in range(WIDTH):
                    energy = octopi[row_index][col_index]
                    if energy > 9:
                        found_flashes = True
                        # Found flash
                        # Set to -1 to not flash twice in a step
                        # https://barretblake.dev/blog/2021/12/advent-of-code-day11/
                        octopi[row_index][col_index] = -1

                        # Increment surrounding octopi
                        # NW
                        if (
                            row_index > 0
                            and col_index > 0
                            and octopi[row_index - 1][col_index - 1] > -1
                        ):
                            octopi[row_index - 1][col_index - 1] += 1
                        # N
                        if row_index > 0 and octopi[row_index - 1][col_index] > -1:
                            octopi[row_index - 1][col_index] += 1
                        # NE
                        if (
                            row_index > 0
                            and col_index < (WIDTH - 1)
                            and octopi[row_index - 1][col_index + 1] > -1
                        ):
                            octopi[row_index - 1][col_index + 1] += 1
                        # E
                        if (
                            col_index < (WIDTH - 1)
                            and octopi[row_index][col_index + 1] > -1
                        ):
                            octopi[row_index][col_index + 1] += 1
                        # SE
                        if (
                            row_index < (HEIGHT - 1)
                            and col_index < (WIDTH - 1)
                            and octopi[row_index + 1][col_index + 1] > -1
                        ):
                            octopi[row_index + 1][col_index + 1] += 1
                        # S
                        if (
                            row_index < (HEIGHT - 1)
                            and octopi[row_index + 1][col_index] > -1
                        ):
                            octopi[row_index + 1][col_index] += 1
                        # SW
                        if (
                            row_index < (HEIGHT - 1)
                            and col_index > 0
                            and octopi[row_index + 1][col_index - 1] > -1
                        ):
                            octopi[row_index + 1][col_index - 1] += 1
                        # W
                        if col_index > 0 and octopi[row_index][col_index - 1] > -1:
                            octopi[row_index][col_index - 1] += 1

        # Set -1's to 0's
        octopi = list(
            map(
                lambda row: list(map(lambda energy: energy if energy >= 0 else 0, row)),
                octopi,
            )
        )

        if all(map(lambda row: all(map(lambda energy: energy == 0, row)), octopi)):
            simul_flash = step
            break
    sys.stdout.write(str(simul_flash))