From 4418292dc4ccd3cae4c414263900351d644a0496 Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Sat, 23 Jan 2021 22:34:58 +0100 Subject: [PATCH] Implement 2019 day 8 --- 2019/aoc2019/__main__.py | 4 ++-- 2019/aoc2019/day06.py | 2 +- 2019/aoc2019/day07.py | 8 ++++---- 2019/aoc2019/day08.py | 33 +++++++++++++++++++++++++++++++++ 2019/aoc2019/intcode.py | 9 +++------ 2019/requirements.txt | 1 + 6 files changed, 44 insertions(+), 13 deletions(-) create mode 100644 2019/aoc2019/day08.py diff --git a/2019/aoc2019/__main__.py b/2019/aoc2019/__main__.py index 50b3aec..91147cf 100644 --- a/2019/aoc2019/__main__.py +++ b/2019/aoc2019/__main__.py @@ -15,9 +15,9 @@ def main() -> None: day = importlib.import_module(f'.day{args.day:02d}', __package__) if args.part2: - function = day.part2 + function = day.part2 # type: ignore else: - function = day.part1 + function = day.part1 # type: ignore print(function(args.input)) diff --git a/2019/aoc2019/day06.py b/2019/aoc2019/day06.py index f35a7aa..b06601b 100644 --- a/2019/aoc2019/day06.py +++ b/2019/aoc2019/day06.py @@ -1,6 +1,6 @@ from typing import TextIO -import networkx +import networkx # type: ignore def read_graph(data: TextIO) -> networkx.DiGraph: diff --git a/2019/aoc2019/day07.py b/2019/aoc2019/day07.py index b923273..ff24b5a 100644 --- a/2019/aoc2019/day07.py +++ b/2019/aoc2019/day07.py @@ -4,8 +4,8 @@ from typing import List, TextIO, Tuple from aoc2019.intcode import read_program, Computer -def amplify(phases: Tuple[int], program: List[int]) -> int: - amps = [] +def amplify(phases: Tuple[int, ...], program: List[int]) -> int: + amps: List[Computer] = [] for i, phase in enumerate(phases): amp = Computer(program.copy()) @@ -25,8 +25,8 @@ def amplify(phases: Tuple[int], program: List[int]) -> int: return amps[-1].output.pop() -def reamplify(phases: Tuple[int], program: List[int]) -> int: - amps = [] +def reamplify(phases: Tuple[int, ...], program: List[int]) -> int: + amps: List[Computer] = [] for i, _ in enumerate(phases): amp = Computer(program.copy()) diff --git a/2019/aoc2019/day08.py b/2019/aoc2019/day08.py new file mode 100644 index 0000000..8511313 --- /dev/null +++ b/2019/aoc2019/day08.py @@ -0,0 +1,33 @@ +from collections import Counter +from typing import Iterable, TextIO + +import numpy # type: ignore + + +def parse_layers(width: int, height: int, data: TextIO) -> Iterable[numpy.array]: + chunk_size = width * height + + content = next(data).strip() + + for pos in range(0, len(content), chunk_size): + yield numpy.array([int(c) for c in content[pos:pos + chunk_size]]) + + +def part1(data: TextIO) -> int: + best_layer: Counter[int] = min((Counter(layer) for layer in parse_layers(25, 6, data)), key=lambda c: c[0]) + + return best_layer[1] * best_layer[2] + + +def format_row(row: Iterable[int]) -> str: + return ''.join('#' if p == 1 else ' ' for p in row) + + +def part2(data: TextIO) -> str: + layers = list(parse_layers(25, 6, data)) + background = numpy.zeros(25 * 6, numpy.int8) + + for layer in reversed(layers): + background[layer != 2] = layer[layer != 2] + + return '\n'.join(format_row(row) for row in background.reshape(6, 25)) diff --git a/2019/aoc2019/intcode.py b/2019/aoc2019/intcode.py index 206fef4..406b28d 100644 --- a/2019/aoc2019/intcode.py +++ b/2019/aoc2019/intcode.py @@ -21,14 +21,11 @@ class Computer: self.output = collections.deque() def _mode_and_key(self, item: Union[int, Tuple[int, int]]) -> Tuple[int, int]: - if type(item) == int: - mode = 0 - key = item + if isinstance(item, int): + return 0, item else: mode, key = item - key = self.program[self.pointer + key] - - return mode, key + return mode, self.program[self.pointer + key] def __getitem__(self, item: Union[int, Tuple[int, int]]) -> int: mode, key = self._mode_and_key(item) diff --git a/2019/requirements.txt b/2019/requirements.txt index 5ff3949..3e5cfd1 100644 --- a/2019/requirements.txt +++ b/2019/requirements.txt @@ -1,2 +1,3 @@ pytest networkx +numpy