Browse Source

Add solution for day 16 part 2 (not necessarily correct)

pull/1/head
Maya Herrscher 2 years ago
parent
commit
e51448bd84
  1. 4
      2022/day16/challenge
  2. 43
      2022/day16/code.py

4
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: **

43
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")

Loading…
Cancel
Save