summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTomasz Kramkowski <tomasz@kramkow.ski>2022-12-16 15:53:44 +0000
committerTomasz Kramkowski <tomasz@kramkow.ski>2022-12-16 15:53:44 +0000
commit06bd72f41fefb162d3857a834a482d4563c1ce8c (patch)
tree45f338445abe74c9e277b2ce14eed0133981cc03
parentdda21c1793285deb17b8215f0c4478c55930c5be (diff)
downloadaoc2022-06bd72f41fefb162d3857a834a482d4563c1ce8c.tar.gz
aoc2022-06bd72f41fefb162d3857a834a482d4563c1ce8c.tar.xz
aoc2022-06bd72f41fefb162d3857a834a482d4563c1ce8c.zip
16 simplify a bit
-rw-r--r--16.py29
1 files changed, 11 insertions, 18 deletions
diff --git a/16.py b/16.py
index 5ea6748..d56f5bb 100644
--- a/16.py
+++ b/16.py
@@ -46,30 +46,23 @@ def recurse(open_valves, current, flow, time_left):
cflow = sum_flow(open_valves)
cnode = nodes[current]
best = 0
- for neighbour, cost in cnode.neighbours:
- if cost >= time_left:
- best = max(best, flow + cflow * time_left)
- else:
- best = max(
- best,
- recurse(open_valves, neighbour,
- flow + cflow * cost, time_left - cost)
- )
+ def loop():
+ nonlocal best
+ for neighbour, cost in cnode.neighbours:
+ if cost >= time_left:
+ best = max(best, flow + cflow * time_left)
+ else:
+ new = recurse(open_valves, neighbour,
+ flow + cflow * cost, time_left - cost)
+ best = max(best, new)
+ loop()
if current not in open_valves and time_left > 0 and cnode.flow > 0:
flow += cflow
open_valves |= {current}
cflow = sum_flow(open_valves)
best = max(best, flow)
time_left -= 1
- for neighbour, cost in cnode.neighbours:
- if cost >= time_left:
- best = max(best, flow + cflow * time_left)
- else:
- best = max(
- best,
- recurse(open_valves, neighbour,
- flow + cflow * cost, time_left - cost)
- )
+ loop()
return best
print(recurse(frozenset(), 'AA', 0, 30))