Solve day 8 part 2

This commit is contained in:
Gabriel Augendre 2020-03-06 17:30:19 +01:00
parent 2913ed34c5
commit 8c3b6556a7
No known key found for this signature in database
GPG key ID: 1E693F4CE4AEE7B4

View file

@ -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__":