From e51448bd8409893480e9cc9710385d594cc84300 Mon Sep 17 00:00:00 2001 From: Maya Herrscher Date: Sat, 17 Dec 2022 12:14:30 +0100 Subject: [PATCH] Add solution for day 16 part 2 (not necessarily correct) --- 2022/day16/challenge | 4 ++++ 2022/day16/code.py | 43 ++++++++++++++++++++++++++++++------------- 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/2022/day16/challenge b/2022/day16/challenge index c913d63..31de3b9 100644 --- a/2022/day16/challenge +++ b/2022/day16/challenge @@ -227,3 +227,7 @@ Valves BB, CC, DD, EE, HH, and JJ are open, releasing 81 pressure. With the elephant helping, after 26 minutes, the best you could do would release a total of 1707 pressure. With you and an elephant working together for 26 minutes, what is the most pressure you could release? + +Your puzzle answer was 2675. + +Both parts of this puzzle are complete! They provide two gold stars: ** diff --git a/2022/day16/code.py b/2022/day16/code.py index 0dadce7..388ff5c 100755 --- a/2022/day16/code.py +++ b/2022/day16/code.py @@ -44,7 +44,7 @@ def find_path(G,s,k): d = deepcopy(di) return max(map(max,d.values())) -def Init_double(G,s,k): +def Init2(G,s,k): d = {} o = {} for v in G['V']: @@ -53,24 +53,42 @@ def Init_double(G,s,k): d[s] = [0] * k return d, o -def anti_relax_double(u,v,w,i,d,k,o): +def anti_relax2(u,v,w,i,d,k,o): if d[v][i] < d[u][i-1] + w[(u,v)] * (k-i): d[v][i] = d[u][i-1] + w[(u,v)] * (k-i) - o[v][i] = o[u][i-1] + [u] + o[v][i] = o[u][i-1] + eval(u) -def find_double_path(G,s,k): - d,o = Init_double(G,s,k) +def find_path2(G,s,k): + d,o = Init2(G,s,k) for i in range(k): di = deepcopy(d) for (u,v) in G['E']: - if d[u][i-1] > -1 and not (u[:2] == v[:2] and v in o[u][i-1]): - anti_relax_double(u,v,G['w'],i,di,k,o) - for (u,v) in G['E']: - if d[u][i-1] > -1 and not (u[:2] == v[:2] and (v in o[u][i-1] or v in o[u][i])): - anti_relax_double(u,v,G['w'],i,di,k,o) + u1,u2 = eval(u) + v1,v2 = eval(v) + prob = False + for (a,b) in [(u1,v1),(u1,v2),(u2,v1),(u2,v2)]: + if a[:2] == b[:2]: + if a in o[u][i-1]: + prob = True + if d[u][i-1] > -1 and not prob: anti_relax2(u,v,G['w'],i,di,k,o) d = deepcopy(di) return max(map(max,d.values())) +def find_double_path(G,s,k): + G2 = {'V': [], 'E': [], 'w': {}} + for u in G['V']: + for v in G['V']: + if u == v and u[2] == 'O': continue + G2['V'].append(str(sorted([u,v]))) + for e in G['E']: + if e[0] == u: + for f in G['E']: + if e[1] == f[1] and e[1][2] == 'O': continue + if f[0] == v: + G2['E'].append((str(sorted([u,v])),str(sorted([e[1],f[1]])))) + G2['w'][(str(sorted([u,v])),str(sorted([e[1],f[1]])))] = G['w'][e] + G['w'][f] + return find_path2(G2,f"['{s}', '{s}']",k) + if __name__ == '__main__': [parse(line.strip('\n')) for line in open(sys.argv[1])] @@ -85,8 +103,7 @@ if __name__ == '__main__': # challenge 2 m = find_double_path(VALVES,'AAC',25) - # missing: magic! - print(m) - res2 = "" + # missing: magic! Doesn't work for example! (off by one) But does work for input... + res2 = str(m) print("challenge 2:" + "\n" + res2 + "\n")