|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +"""Advent of Code Programming Puzzles |
| 4 | +
|
| 5 | +2021 Edition - Day 11 |
| 6 | +Puzzle Solution in Python |
| 7 | +""" |
| 8 | + |
| 9 | +import copy |
| 10 | +import logging |
| 11 | +import sys |
| 12 | +import time |
| 13 | +from collections import namedtuple |
| 14 | + |
| 15 | +from pathlib import Path |
| 16 | +from typing import Iterator |
| 17 | + |
| 18 | +from common.support import parse_arguments, configure_logger |
| 19 | +from itertools import product |
| 20 | + |
| 21 | +log = logging.getLogger(__name__) |
| 22 | + |
| 23 | +DEFAULT_FILE = 'example-input.txt' |
| 24 | +DEFAULT_FILE = 'input.txt' |
| 25 | +EXIT_SUCCESS = 0 |
| 26 | + |
| 27 | +Position = namedtuple('Position', ['col', 'row']) |
| 28 | + |
| 29 | + |
| 30 | +def load_contents(filename: Path) -> dict[Position: int]: |
| 31 | + """Load and convert contents from file |
| 32 | +
|
| 33 | + :param filename: input filename |
| 34 | + :return: contents structured as a map |
| 35 | + """ |
| 36 | + |
| 37 | + with open(filename, encoding='utf-8') as buffer: |
| 38 | + contents = {Position(col, row): |
| 39 | + {"energy": int(level), "flashes": 0, "flashed": False} |
| 40 | + for row, line in enumerate(buffer.readlines()) |
| 41 | + for col, level in enumerate(line.strip())} |
| 42 | + return contents |
| 43 | + |
| 44 | + |
| 45 | +AXIS = 2 |
| 46 | +STENCILS = tuple(c for c in product((-1, 0, 1), repeat=AXIS) if any(c)) |
| 47 | + |
| 48 | + |
| 49 | +def compute_neighbors(pos: tuple[int]) -> tuple: |
| 50 | + """Compute neighbors |
| 51 | +
|
| 52 | + :param pos: point coordinates |
| 53 | + :return: list of neighbors |
| 54 | + """ |
| 55 | + neighbors = tuple(tuple([sum(x) for x in zip(pos, s)]) for s in STENCILS) |
| 56 | + return neighbors |
| 57 | + |
| 58 | + |
| 59 | +def solve_first_part(contents: any) -> int: |
| 60 | + """Solve the first part of the challenge |
| 61 | +
|
| 62 | + :param contents: decoded contents |
| 63 | + :return: answer for the first part |
| 64 | + """ |
| 65 | + |
| 66 | + THRESHOLD = 9 |
| 67 | + |
| 68 | + def step(octopuses_: dict) -> dict: |
| 69 | + # increment energy level of all octopuses_ |
| 70 | + for pos in octopuses_: |
| 71 | + octopuses_[pos]['energy'] += 1 |
| 72 | + octopuses_[pos]['flashed'] = False |
| 73 | + while any(v['energy'] > THRESHOLD for v in octopuses_.values()): |
| 74 | + # propagate flushing to neighbors |
| 75 | + for pos in octopuses_: |
| 76 | + flushing = octopuses_[pos]['energy'] > THRESHOLD and not octopuses_[pos]['flashed'] |
| 77 | + if not flushing: |
| 78 | + continue |
| 79 | + for n in compute_neighbors(pos=pos): |
| 80 | + if n in octopuses_: |
| 81 | + if not octopuses_[n]['flashed']: |
| 82 | + octopuses_[n]['energy'] += 1 |
| 83 | + octopuses_[pos]['energy'] = 0 |
| 84 | + octopuses_[pos]['flashes'] += 1 |
| 85 | + octopuses_[pos]['flashed'] = True |
| 86 | + return octopuses_ |
| 87 | + |
| 88 | + octopuses = contents |
| 89 | + STEPS = 100 |
| 90 | + for i in range(STEPS): |
| 91 | + octopuses = step(octopuses_=octopuses) |
| 92 | + answer = sum(v['flashes'] for v in octopuses.values()) |
| 93 | + return answer |
| 94 | + |
| 95 | + |
| 96 | +def dump_octopuses_levels(octopuses: dict) -> None: |
| 97 | + row = 0 |
| 98 | + while any(row in p for p in octopuses): |
| 99 | + col = 0 |
| 100 | + while any(col in p for p in octopuses): |
| 101 | + energy = octopuses[Position(col=col, row=row)]['energy'] |
| 102 | + print(energy if energy < 10 else '#', end='') |
| 103 | + col += 1 |
| 104 | + print('') |
| 105 | + row += 1 |
| 106 | + |
| 107 | + |
| 108 | +def main() -> int: |
| 109 | + """Script main method |
| 110 | +
|
| 111 | + :return: script exit code returned to the shell |
| 112 | + """ |
| 113 | + |
| 114 | + args = parse_arguments() |
| 115 | + configure_logger(verbose=args.verbose) |
| 116 | + log.debug(f'called with {args=}') |
| 117 | + |
| 118 | + start_time = time.perf_counter() |
| 119 | + |
| 120 | + contents = load_contents(filename=args.filename) |
| 121 | + compute_first_part = \ |
| 122 | + 'solve_first_part' in globals() and (not args.part or args.part == 1) |
| 123 | + compute_second_part = \ |
| 124 | + 'solve_second_part' in globals() and (not args.part or args.part == 2) |
| 125 | + answer_part_one = \ |
| 126 | + solve_first_part(contents=contents) if compute_first_part else 0 |
| 127 | + answer_part_two = \ |
| 128 | + solve_second_part(contents=contents) if compute_second_part else 0 |
| 129 | + |
| 130 | + elapsed_time = time.perf_counter() - start_time |
| 131 | + |
| 132 | + print(f'{answer_part_one=}') |
| 133 | + print(f'{answer_part_two=}') |
| 134 | + print(f'done in {1000 * elapsed_time:0.1f} milliseconds') |
| 135 | + |
| 136 | + return EXIT_SUCCESS |
| 137 | + |
| 138 | + |
| 139 | +if __name__ == '__main__': |
| 140 | + """Standalone entry-point""" |
| 141 | + NO_INPUT_FILE = 1 == len(sys.argv) |
| 142 | + if NO_INPUT_FILE: |
| 143 | + default_input = Path(__file__).parent / DEFAULT_FILE |
| 144 | + sys.argv.append(str(default_input)) |
| 145 | + sys.exit(main()) |
0 commit comments