advent-of-code/2019/day08_image.py

48 lines
1,011 B
Python
Raw Normal View History

2020-03-06 17:30:19 +01:00
WIDTH = 25
HEIGHT = 6
LAYER_SIZE = WIDTH * HEIGHT
WHITE = "1"
BLACK = "0"
TRANSPARENT = "2"
2022-12-02 13:35:44 +01:00
def find_pixel(layers: list[str], index: int) -> str:
2020-03-06 17:30:19 +01:00
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="")
2020-03-06 17:19:52 +01:00
def main():
with open("inputs/day08") as f:
data = f.readline().strip()
layers = []
position = 0
2020-03-06 17:30:19 +01:00
layer = data[position : position + LAYER_SIZE]
2020-03-06 17:19:52 +01:00
while layer:
layers.append(layer)
2020-03-06 17:30:19 +01:00
position += LAYER_SIZE
layer = data[position : position + LAYER_SIZE]
result_image = ""
for index in range(LAYER_SIZE):
result_image += find_pixel(layers, index)
2020-03-06 17:19:52 +01:00
2020-03-06 17:30:19 +01:00
display_image(result_image)
2020-03-06 17:19:52 +01:00
if __name__ == "__main__":
main()