summaryrefslogtreecommitdiffstats
path: root/16.py
diff options
context:
space:
mode:
Diffstat (limited to '16.py')
-rw-r--r--16.py22
1 files 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())