From 4fa1b1e14cb0ce7245849cb8a9c4c144656d639a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20Engl=C3=B6f=20Ytterstr=C3=B6m?= Date: Tue, 12 Dec 2023 15:11:26 +0100 Subject: [PATCH] Solve 2023:12 "Hot Springs" --- 2023-python/output/day_12.py | 64 ++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 2023-python/output/day_12.py diff --git a/2023-python/output/day_12.py b/2023-python/output/day_12.py new file mode 100644 index 0000000..d0cc2a3 --- /dev/null +++ b/2023-python/output/day_12.py @@ -0,0 +1,64 @@ +from functools import cache +from output import answer + +n = 12 +title = "Hot Springs" + + +@answer(1, "sum of all possible combinations is {}") +def part_1(presolved): + return presolved[0] + + +@answer(2, "sum of all possible combinations is {} when unfolded") +def part_2(presolved): + return presolved[1] + + +def presolve(data): + lines = [ + (a, list(map(int, b.split(",")))) + for a, b in (line.split() for line in data.splitlines()) + ] + p1 = sum(_inspect(a, tuple(b)) for a, b in lines) + p2 = sum(_inspect("?".join([a] * 5), tuple(b * 5)) for a, b in lines) + return p1, p2 + + +@cache +def _inspect(s, cs): + r = len(s) + csl = len(cs) + if r == 0: + return 1 if csl == 0 else 0 + o, *f = s + f = "".join(f) + if o == ".": + return _inspect(f, cs) + if o == "?": + return _inspect("." + f, cs) + _inspect("#" + f, cs) + if not csl: + return 0 + g = cs[0] + if g > r or "." in s[0:g]: + return 0 + elif csl > 1: + if g + 1 > r or s[g] == "#": + return 0 + else: + return _inspect(s[g + 1 :], cs[1:]) + elif csl == 1: + return _inspect(s[g:], ()) + + +if __name__ == "__main__": + with open("./input/12.txt", "r") as f: + inp = f.read().strip() + + inp = presolve(inp) + + a = part_1(inp) + b = part_2(inp) + + assert a == 7118 + assert b == 7030194981795