From 96c4c1383a41d37c0865570913f1e8d99ff697d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20Engl=C3=B6f=20Ytterstr=C3=B6m?= Date: Sat, 9 Dec 2023 16:34:08 +0100 Subject: [PATCH] Solve 2023:09 "Mirage Maintenance" --- 2023-python/output/day_09.py | 39 ++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 2023-python/output/day_09.py diff --git a/2023-python/output/day_09.py b/2023-python/output/day_09.py new file mode 100644 index 0000000..0cd5ad5 --- /dev/null +++ b/2023-python/output/day_09.py @@ -0,0 +1,39 @@ +from output import answer, puzzleinput + +n = 9 +title = "Mirage Maintenance" + + +@answer(1, "OASIS report extrapolated values sum is {}") +def part_1(data): + lines = [[int(d) for d in line.split()] for line in data.splitlines()] + return _solve(lines) + + +@answer(2, "Using prepending, OASIS report extrapolated values sum is {}") +def part_2(data): + lines = [[int(d) for d in line.split()[::-1]] for line in data.splitlines()] + return _solve(lines) + + +def _solve(lines): + vs = 0 + for l in lines: + h = [l] + while any(n != 0 for n in h[-1]): + h.append([b - a for a, b in zip(h[-1], h[-1][1:])]) + h = h[::-1] + for i in range(len(h)): + h[i].append(0 if i == 0 else h[i - 1][-1] + h[i][-1]) + vs += h[-1][-1] + return vs + + +if __name__ == "__main__": + inp = parse_input() + + a = part_1(inp) + b = part_2(inp) + + assert a == 1702218515 + assert b == 925