day 16 python part 1 solve

this shouldn't work
main
magical 2022-12-15 22:54:16 -08:00
parent f39fbe1890
commit 03710f579a
1 changed files with 44 additions and 15 deletions

View File

@ -13,37 +13,66 @@ for line in open("input"):
print(G) print(G)
def save():
seen = set()
with open("input.dot", "w") as f:
print("graph {", file=f)
for v, r, E in G:
if r:
print('%s [label="%s (%s)"]' % (v, v, r), file=f)
for e in E:
if (e,v) not in seen:
print("%s -- %s" % (v,e), file=f)
seen.add((v,e))
print("}", file=f)
#save()
#import astar #import astar
score = {'AA': (0, frozenset())} score = {'AA': (0, 0, [])}
#E = {x[0]: x[2] for x in G}
#assert all(v in E[e] for v in E for e in E[v])
# find the largest reward we can get in n steps # find the largest reward we can get in n steps
# then find the largest reward we can get in n+1 steps # then find the largest reward we can get in n+1 steps
potential = sum(r for _,r,_ in G)
minutes = 30 minutes = 30
for _ in range(30): for _ in range(minutes):
minutes -= 1 minutes -= 1
next = {} next = {}
for v, r, E in G: for v, r, E in G:
s = set() vo = v + 'o'
s = []
o = []
if vo in score:
reward, flow, open = score[vo]
o.append((reward, flow, open))
if v in score: if v in score:
reward, open = score[v] reward, flow, open = score[v]
# stay, don't open valve # stay, don't open valve
s.add((reward, open)) s.append((reward, flow, open))
# stay in place, open valve # stay in place, open valve
if v not in open: if v not in open:
reward += r*minutes reward += r*minutes
open = open.union(frozenset([v])) o.append((reward, flow+r, open+[v]))
s.add((reward, open))
# move here from somewhere else # move here from somewhere else
for e in E: for e in E:
if e in score: if e in score:
s.add(score[e]) if v in score[e][-1]:
o.append(score[e])
else:
s.append(score[e])
eo = e+'o'
if eo in score:
if v in score[eo][-1]:
o.append(score[eo])
else:
s.append(score[eo])
if s: if s:
reward, open = max(s) next[v] = max(s)
next[v] = (reward,open) if o:
next[vo] = max(o)
score = next score = next
print(score) print("%d minutes left" % minutes)
print(max(r for r,_ in score.values())) for v, (r, flow, open) in sorted(score.items(), key=lambda x: x[1]):
print("\t", v, r, "\t", ",".join(open))
print(max(r for r,_,_ in score.values()))