From b52eef5b9399ab3888fcfe1bc9ca23c7a9f7b026 Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Wed, 10 Dec 2025 18:15:14 +0100 Subject: Day 10 2025 --- 2025/10/b-recursive-slow.py | 66 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 2025/10/b-recursive-slow.py (limited to '2025/10/b-recursive-slow.py') diff --git a/2025/10/b-recursive-slow.py b/2025/10/b-recursive-slow.py new file mode 100644 index 0000000..a52b50e --- /dev/null +++ b/2025/10/b-recursive-slow.py @@ -0,0 +1,66 @@ +import fileinput +from functools import cache + +def readline(line): + b = [] + j = line.index(']') + while '(' in line[j:]: + i = line[j:].index('(') + j + 1 + j = line[i:].index(')') + i + x = 0 + for y in line[i:j].split(','): + x |= 1 << int(y) + b.append(x) + + begin = line.index('{')+1 + end = line.index('}') + j = tuple(int(x) for x in line[begin:end].split(',')) + + return tuple(b), j + +def newj(j, b, n): + return tuple(j[i]-n if b & (1< 0 and not any(can(bb, i) for bb in b): + return inf + + # Optimization: if any of the buttons is the last one available that + # toggles a certain jolt, we press it as much as needed. + for i in range(len(j)): + for k in range(len(b)): + bb = b[:k] + b[k+1:] + if can(b[k], i) and not any(can(x, i) for x in bb): + return j[i] + sol(bb, newj(j, b[k], j[i]), best) + + c = 0 + while all(x >= 0 for x in j) and c <= best: + best = min(best, c + sol(b[1:], j, best)) + j = newj(j, b[0], 1) + c += 1 + + return best + +with fileinput.input() as lines: + sols = [] + c = 1 + for line in lines: + print(f"doing line {c}: {line}") + c += 1 + sols.append(sol(*readline(line), inf)) + print(sols) + print(sum(sols)) + -- cgit v1.3