From 4623fdac95be265d056797d50ae252e1ae3985bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20Ytterstr=C3=B6m?= Date: Thu, 23 Nov 2023 14:49:48 +0100 Subject: [PATCH] Solve 2019:08 "Space Image Format" --- 2019-python/output/__init__.py | 4 +++- 2019-python/output/day_08.py | 38 ++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 2019-python/output/day_08.py diff --git a/2019-python/output/__init__.py b/2019-python/output/__init__.py index addf3ba..7877cc4 100644 --- a/2019-python/output/__init__.py +++ b/2019-python/output/__init__.py @@ -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: diff --git a/2019-python/output/day_08.py b/2019-python/output/day_08.py new file mode 100644 index 0000000..93e495c --- /dev/null +++ b/2019-python/output/day_08.py @@ -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)