DEV Community

Discussion on: Advent of Code 2019 Solution Megathread - Day 8: Space Image Format

Collapse
 
peter_8000 profile image
Piotr

Another Python hack...

Part 1

from collections import Counter

with open("day08.txt") as file:
    raw = file.read().strip()

chunks = []
for idx in range(0, len(raw), 150):
    chunks.append(raw[idx:idx+150])

zero_counts = [Counter(chunk)["0"] for chunk in chunks]
least_zero_layer = counts.index(sorted(zero_counts)[0])
cnt = Counter(chunks[least_zero_layer])
result = cnt["1"] * cnt["2"]

print("Solution Pt.1:", result)

Part 2

image = []

for idx in range(150):
    if idx!= 0 and idx % 25 == 0:
        image.append("\n")
    layer = 0

    while True:
        pixel = int(chunks[layer][idx])
        if pixel == 0 or pixel == 1:
            image.append(str(pixel))
            break
        layer +=1

print("".join(image).replace("0", " "))