diff --git a/day8/py/main.py b/day8/py/main.py index 9d2164f..aba65fb 100644 --- a/day8/py/main.py +++ b/day8/py/main.py @@ -14,12 +14,38 @@ def make_layers(input: Tuple[int], width: int, height: int) -> Tuple[Tuple[int]] return tuple(layers) +def stack_layers(layers: Tuple[Tuple[str]], width: int, height: int) -> Tuple[int]: + merged_stacks = [] + for i in range(height): + for j in range(width): + stack = [] + for layer in layers: + stack.append(layer[i * width + j]) + try: + pixel = next(item for item in stack if item != 2) + except StopIteration: + pixel = 0 + + merged_stacks.append(pixel) + + return tuple(merged_stacks) + + def part1(layers: Tuple[Tuple[str]]) -> int: fewest_zero_layer = min((layer for layer in layers), key=lambda layer: layer.count(0)) return fewest_zero_layer.count(1) * fewest_zero_layer.count(2) +def part2(layers: Tuple[Tuple[str]], width: int, height: int) -> int: + stacked = stack_layers(layers, width, height) + for i in range(height): + for j in range(width): + pixel = stacked[i * width + j] + print('#' if pixel == 1 else ' ', end=' ') + print('') + + if __name__ == "__main__": if len(sys.argv) != 4: print("Usage: ./main.py in_file width height") @@ -32,4 +58,7 @@ if __name__ == "__main__": height = int(sys.argv[3]) layers = make_layers(input, width, height) + print("PART 1") print(part1(layers)) + print("PART 2") + part2(layers, width, height) diff --git a/day8/sample2.txt b/day8/sample2.txt new file mode 100644 index 0000000..2bca797 --- /dev/null +++ b/day8/sample2.txt @@ -0,0 +1 @@ +0222112222120000