mirror of
https://github.com/Crocmagnon/advent-of-code.git
synced 2024-11-22 06:28:11 +01:00
Solve day 8 part 2
This commit is contained in:
parent
2913ed34c5
commit
8c3b6556a7
1 changed files with 35 additions and 16 deletions
|
@ -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():
|
def main():
|
||||||
width = 25
|
|
||||||
height = 6
|
|
||||||
with open("inputs/day08") as f:
|
with open("inputs/day08") as f:
|
||||||
data = f.readline().strip()
|
data = f.readline().strip()
|
||||||
layers = []
|
layers = []
|
||||||
layer_size = width * height
|
|
||||||
position = 0
|
position = 0
|
||||||
layer = data[position : position + layer_size]
|
layer = data[position : position + LAYER_SIZE]
|
||||||
number_of_zero_digits = math.inf
|
|
||||||
layer_index = -1
|
|
||||||
while layer:
|
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)
|
layers.append(layer)
|
||||||
position += layer_size
|
position += LAYER_SIZE
|
||||||
layer = data[position : position + layer_size]
|
layer = data[position : position + LAYER_SIZE]
|
||||||
|
|
||||||
print(layer_index)
|
result_image = ""
|
||||||
layer = layers[layer_index]
|
for index in range(LAYER_SIZE):
|
||||||
print(layer.count("1") * layer.count("2"))
|
result_image += find_pixel(layers, index)
|
||||||
|
|
||||||
|
display_image(result_image)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
Loading…
Reference in a new issue