From 8690ab12904c2673db74ec9fdfc52f5b8d8bf2dc Mon Sep 17 00:00:00 2001 From: Tomasz Kramkowski Date: Fri, 16 Dec 2022 16:02:07 +0000 Subject: 16 much shorter --- 16.py | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/16.py b/16.py index acddc1f..779b8aa 100644 --- a/16.py +++ b/16.py @@ -41,22 +41,14 @@ def sum_flow(open_valves): def recurse(open_valves=frozenset(), current='AA', flow=0, time_left=30): cflow = sum_flow(open_valves) cnode = nodes[current] - best = 0 - def loop(): - nonlocal best - best = max(best, flow + cflow * time_left) - for neighbour, cost in cnode.neighbours: - if cost >= time_left: continue - new = recurse(open_valves, neighbour, - flow + cflow * cost, time_left - cost) - best = max(best, new) - loop() + best = flow + cflow * time_left + for neighbour, cost in cnode.neighbours: + if cost >= time_left: continue + new = recurse(open_valves, neighbour, + flow + cflow * cost, time_left - cost) + best = max(best, new) if current not in open_valves and time_left > 0 and cnode.flow > 0: - flow += cflow - open_valves |= {current} - cflow = sum_flow(open_valves) - time_left -= 1 - loop() + best = max(best, recurse(open_valves | {current}, current, flow + cflow, time_left - 1)) return best print(recurse()) -- cgit v1.2.3-54-g00ecf