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) #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]) # find the largest reward we can get in n steps # then find the largest reward we can get in n+1 steps minutes = 30 for _ in range(30): minutes -= 1 next = {} for v, r, E in G: s = set() if v in score: reward, open = score[v] # stay, don't open valve s.add((reward, open)) # stay in place, open valve if v not in open: reward += r*minutes open = open.union(frozenset([v])) s.add((reward, open)) # move here from somewhere else for e in E: if e in score: s.add(score[e]) if s: reward, open = max(s) next[v] = (reward,open) score = next print(score) print(max(r for r,_ in score.values()))