summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTomasz Kramkowski <tomasz@kramkow.ski>2022-12-16 16:02:07 +0000
committerTomasz Kramkowski <tomasz@kramkow.ski>2022-12-16 16:02:07 +0000
commit8690ab12904c2673db74ec9fdfc52f5b8d8bf2dc (patch)
tree96b4c7df5b50b6ef694d746bc046e0e76cd011f1
parentfac0012669169e48732283f6daa72f73f65ee84d (diff)
downloadaoc2022-8690ab12904c2673db74ec9fdfc52f5b8d8bf2dc.tar.gz
aoc2022-8690ab12904c2673db74ec9fdfc52f5b8d8bf2dc.tar.xz
aoc2022-8690ab12904c2673db74ec9fdfc52f5b8d8bf2dc.zip
16 much shorter
-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())