parent
f39fbe1890
commit
03710f579a
59
day16/sol.py
59
day16/sol.py
|
@ -13,37 +13,66 @@ for line in open("input"):
|
|||
|
||||
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
|
||||
|
||||
score = {'AA': (0, frozenset())}
|
||||
|
||||
#E = {x[0]: x[2] for x in G}
|
||||
#assert all(v in E[e] for v in E for e in E[v])
|
||||
score = {'AA': (0, 0, [])}
|
||||
|
||||
# find the largest reward we can get in n steps
|
||||
# then find the largest reward we can get in n+1 steps
|
||||
potential = sum(r for _,r,_ in G)
|
||||
minutes = 30
|
||||
for _ in range(30):
|
||||
for _ in range(minutes):
|
||||
minutes -= 1
|
||||
next = {}
|
||||
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:
|
||||
reward, open = score[v]
|
||||
reward, flow, open = score[v]
|
||||
# stay, don't open valve
|
||||
s.add((reward, open))
|
||||
s.append((reward, flow, open))
|
||||
# stay in place, open valve
|
||||
if v not in open:
|
||||
reward += r*minutes
|
||||
open = open.union(frozenset([v]))
|
||||
s.add((reward, open))
|
||||
o.append((reward, flow+r, open+[v]))
|
||||
# move here from somewhere else
|
||||
for e in E:
|
||||
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:
|
||||
reward, open = max(s)
|
||||
next[v] = (reward,open)
|
||||
next[v] = max(s)
|
||||
if o:
|
||||
next[vo] = max(o)
|
||||
score = next
|
||||
print(score)
|
||||
print(max(r for r,_ in score.values()))
|
||||
print("%d minutes left" % minutes)
|
||||
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()))
|
||||
|
|
Loading…
Reference in New Issue