From 03710f579a82432703e57f1dff7298a404453da1 Mon Sep 17 00:00:00 2001 From: Andrew Ekstedt Date: Thu, 15 Dec 2022 22:54:16 -0800 Subject: [PATCH] day 16 python part 1 solve this shouldn't work --- day16/sol.py | 59 +++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 44 insertions(+), 15 deletions(-) diff --git a/day16/sol.py b/day16/sol.py index d5aa014..163307d 100644 --- a/day16/sol.py +++ b/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()))