Solve 2019:08 "Space Image Format"

This commit is contained in:
Anders Englöf Ytterström 2023-11-23 14:49:48 +01:00 committed by Anders Englöf Ytterström
parent 5be719dcf9
commit 4623fdac95
2 changed files with 41 additions and 1 deletions

View file

@ -25,7 +25,9 @@ def answer(part_index, fmt_string):
@functools.wraps(func)
def wrapper_aoc(*args, **kwargs):
decorate = kwargs.get("decorate", False)
answer = func(*args)
if decorate:
del kwargs["decorate"]
answer = func(*args, **kwargs)
if not decorate:
print(answer)
else:

View file

@ -0,0 +1,38 @@
from collections import Counter
from textwrap import wrap
from output import answer, puzzleinput
n = 8
title = "Space Image Format"
@puzzleinput(n)
def parse_input(data):
return data
@answer(1, "the product of all 1s and 2s in the layer with fewest 0s is {}")
def part_1(data):
layers = sorted(map(Counter, wrap(data, 25 * 6)), key=lambda c: c["0"])
a = layers[0]["1"]
b = layers[0]["2"]
return a * b
@answer(2, "The message is CYUAH, the decoded image looks like this:\n\n{}")
def part_2(data, width=25, height=6):
layers = wrap(data, width * height)
l = len(layers)
pixels = zip(*layers)
lit = map(
lambda s: s.replace("0", ".").replace("1", "#"),
map(lambda p: next(filter(lambda x: x != "2", p)), pixels),
)
matrix = "\n".join(wrap("".join(lit), width))
return matrix
if __name__ == "__main__":
parsed = parse_input()
part_1(parsed)
part_2(parsed)