2022-12-16 06:53:55 +00:00
|
|
|
G = []
|
|
|
|
for line in open("input"):
|
|
|
|
words = line.split()
|
|
|
|
valve = words[1]
|
|
|
|
rate = words[4]
|
|
|
|
edges = [x.strip(", ") for x in words[9:]]
|
|
|
|
|
|
|
|
print(rate)
|
|
|
|
rate = rate.strip(";")
|
|
|
|
rate = rate[rate.index("=")+1:]
|
|
|
|
rate = int(rate)
|
|
|
|
G.append((valve, rate, edges))
|
|
|
|
|
|
|
|
print(G)
|
|
|
|
|
2022-12-16 06:54:16 +00:00
|
|
|
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()
|
2022-12-16 06:53:55 +00:00
|
|
|
|
2022-12-16 06:54:16 +00:00
|
|
|
#import astar
|
2022-12-16 06:53:55 +00:00
|
|
|
|
2022-12-16 06:54:16 +00:00
|
|
|
score = {'AA': (0, 0, [])}
|
2022-12-16 06:53:55 +00:00
|
|
|
|
|
|
|
# find the largest reward we can get in n steps
|
|
|
|
# then find the largest reward we can get in n+1 steps
|
2022-12-16 06:54:16 +00:00
|
|
|
potential = sum(r for _,r,_ in G)
|
2022-12-16 06:53:55 +00:00
|
|
|
minutes = 30
|
2022-12-16 06:54:16 +00:00
|
|
|
for _ in range(minutes):
|
2022-12-16 06:53:55 +00:00
|
|
|
minutes -= 1
|
|
|
|
next = {}
|
|
|
|
for v, r, E in G:
|
2022-12-16 06:54:16 +00:00
|
|
|
vo = v + 'o'
|
|
|
|
s = []
|
|
|
|
o = []
|
|
|
|
if vo in score:
|
|
|
|
reward, flow, open = score[vo]
|
|
|
|
o.append((reward, flow, open))
|
2022-12-16 06:53:55 +00:00
|
|
|
if v in score:
|
2022-12-16 06:54:16 +00:00
|
|
|
reward, flow, open = score[v]
|
2022-12-16 06:53:55 +00:00
|
|
|
# stay, don't open valve
|
2022-12-16 06:54:16 +00:00
|
|
|
s.append((reward, flow, open))
|
2022-12-16 06:53:55 +00:00
|
|
|
# stay in place, open valve
|
|
|
|
if v not in open:
|
|
|
|
reward += r*minutes
|
2022-12-16 06:54:16 +00:00
|
|
|
o.append((reward, flow+r, open+[v]))
|
2022-12-16 06:53:55 +00:00
|
|
|
# move here from somewhere else
|
|
|
|
for e in E:
|
|
|
|
if e in score:
|
2022-12-16 06:54:16 +00:00
|
|
|
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])
|
2022-12-16 06:53:55 +00:00
|
|
|
if s:
|
2022-12-16 06:54:16 +00:00
|
|
|
next[v] = max(s)
|
|
|
|
if o:
|
|
|
|
next[vo] = max(o)
|
2022-12-16 06:53:55 +00:00
|
|
|
score = next
|
2022-12-16 06:54:16 +00:00
|
|
|
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()))
|