From 8c3b6556a7b005c261e6faaf5b226a7c8d1ff56a Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Fri, 6 Mar 2020 17:30:19 +0100 Subject: [PATCH] Solve day 8 part 2 --- 2019/day08-image.py | 51 +++++++++++++++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 16 deletions(-) diff --git a/2019/day08-image.py b/2019/day08-image.py index f9f52be..5772a2d 100644 --- a/2019/day08-image.py +++ b/2019/day08-image.py @@ -1,29 +1,48 @@ -import math +from typing import List + +WIDTH = 25 +HEIGHT = 6 +LAYER_SIZE = WIDTH * HEIGHT + +WHITE = "1" +BLACK = "0" +TRANSPARENT = "2" + + +def find_pixel(layers: List[str], index: int) -> str: + for layer in layers: + pixel = layer[index] + if pixel in [WHITE, BLACK]: + return pixel + return TRANSPARENT + + +def display_image(image: str) -> None: + for i, pixel in enumerate(image): + if i % WIDTH == 0: + print() + character = " " + if pixel == WHITE: + character = "█" + print(character, end="") def main(): - width = 25 - height = 6 with open("inputs/day08") as f: data = f.readline().strip() layers = [] - layer_size = width * height position = 0 - layer = data[position : position + layer_size] - number_of_zero_digits = math.inf - layer_index = -1 + layer = data[position : position + LAYER_SIZE] while layer: - count = layer.count("0") - if count < number_of_zero_digits: - layer_index = int(position / layer_size) - number_of_zero_digits = count layers.append(layer) - position += layer_size - layer = data[position : position + layer_size] + position += LAYER_SIZE + layer = data[position : position + LAYER_SIZE] - print(layer_index) - layer = layers[layer_index] - print(layer.count("1") * layer.count("2")) + result_image = "" + for index in range(LAYER_SIZE): + result_image += find_pixel(layers, index) + + display_image(result_image) if __name__ == "__main__":