Frans Bergman 2 жил өмнө
parent
commit
ea6fc00695
2 өөрчлөгдсөн 119 нэмэгдсэн , 0 устгасан
  1. 50 0
      16.input
  2. 69 0
      16.py

+ 50 - 0
16.input

@@ -0,0 +1,50 @@
+Valve EV has flow rate=0; tunnels lead to valves WG, IB
+Valve IB has flow rate=0; tunnels lead to valves EW, EV
+Valve KL has flow rate=0; tunnels lead to valves JH, OY
+Valve QJ has flow rate=0; tunnels lead to valves TX, JH
+Valve OA has flow rate=12; tunnels lead to valves SB, GI, ED
+Valve BQ has flow rate=0; tunnels lead to valves NK, JJ
+Valve PZ has flow rate=0; tunnels lead to valves JH, VA
+Valve QO has flow rate=8; tunnels lead to valves LN, LU, CU, SQ, YZ
+Valve MP has flow rate=0; tunnels lead to valves LN, GO
+Valve YZ has flow rate=0; tunnels lead to valves AA, QO
+Valve CU has flow rate=0; tunnels lead to valves RY, QO
+Valve UE has flow rate=16; tunnel leads to valve VP
+Valve HT has flow rate=0; tunnels lead to valves AA, JE
+Valve EF has flow rate=0; tunnels lead to valves ES, JE
+Valve JJ has flow rate=15; tunnel leads to valve BQ
+Valve JX has flow rate=0; tunnels lead to valves AA, GO
+Valve AA has flow rate=0; tunnels lead to valves JX, TX, HT, YZ
+Valve MI has flow rate=21; tunnels lead to valves PQ, QT
+Valve ES has flow rate=0; tunnels lead to valves EF, NK
+Valve VC has flow rate=0; tunnels lead to valves MC, IW
+Valve LN has flow rate=0; tunnels lead to valves MP, QO
+Valve ED has flow rate=0; tunnels lead to valves OA, RY
+Valve WG has flow rate=20; tunnels lead to valves EV, OY, KF
+Valve GI has flow rate=0; tunnels lead to valves WE, OA
+Valve UK has flow rate=0; tunnels lead to valves TO, JE
+Valve GY has flow rate=23; tunnels lead to valves EO, QT
+Valve TX has flow rate=0; tunnels lead to valves AA, QJ
+Valve OE has flow rate=0; tunnels lead to valves GO, NK
+Valve OQ has flow rate=9; tunnels lead to valves VP, SB
+Valve NK has flow rate=25; tunnels lead to valves OE, ES, BQ
+Valve LU has flow rate=0; tunnels lead to valves JH, QO
+Valve RY has flow rate=18; tunnels lead to valves ED, IW, CU
+Valve KF has flow rate=0; tunnels lead to valves JE, WG
+Valve IW has flow rate=0; tunnels lead to valves VC, RY
+Valve SQ has flow rate=0; tunnels lead to valves MC, QO
+Valve PQ has flow rate=0; tunnels lead to valves MC, MI
+Valve TO has flow rate=0; tunnels lead to valves UK, JH
+Valve OY has flow rate=0; tunnels lead to valves KL, WG
+Valve JE has flow rate=10; tunnels lead to valves EF, ND, HT, KF, UK
+Valve JH has flow rate=3; tunnels lead to valves QJ, KL, PZ, TO, LU
+Valve VP has flow rate=0; tunnels lead to valves OQ, UE
+Valve EW has flow rate=22; tunnel leads to valve IB
+Valve ND has flow rate=0; tunnels lead to valves JE, GO
+Valve VA has flow rate=0; tunnels lead to valves GO, PZ
+Valve QT has flow rate=0; tunnels lead to valves MI, GY
+Valve EO has flow rate=0; tunnels lead to valves GY, MC
+Valve MC has flow rate=11; tunnels lead to valves PQ, SQ, WE, EO, VC
+Valve GO has flow rate=4; tunnels lead to valves JX, VA, OE, MP, ND
+Valve SB has flow rate=0; tunnels lead to valves OQ, OA
+Valve WE has flow rate=0; tunnels lead to valves MC, GI

+ 69 - 0
16.py

@@ -0,0 +1,69 @@
+from queue import PriorityQueue
+
+lines = [line.split() for line in open("16.input")]
+
+valves = {line[1]: (int(line[4].split("=")[1][:-1]), [n.replace(",", "") for n in line[9:]]) for line in lines}
+
+useful_valves = set(k for k, v in valves.items() if v[0] > 0)
+
+# Find all relevant distances
+dists = {}
+for a in list(useful_valves) + ["AA"]:
+    q = PriorityQueue()
+    dists[(a, a)] = 0
+    q.put((0, a))
+
+    while not q.empty():
+        _, current = q.get()
+        dist = dists[(a, current)]
+        _, nbrs = valves[current]
+        for n in nbrs:
+            old_dist = dists.get((a, n), 100000)
+            if old_dist > dist + 1:
+                dists[(a, n)] = dist + 1
+                q.put((dists[(a, n)], n))
+
+
+def search(currents, opened, remainings, score, paths):
+    old_score = score_cache.get((currents, remainings), -1)
+
+    if score > old_score:
+        score_cache[(currents, remainings)] = score
+    else:
+        return score
+
+    nexts = useful_valves - opened
+
+    scores = [score]
+
+    _, i = max((rem, i) for i, rem in enumerate(remainings))
+    current = currents[i]
+    remaining = remainings[i]
+
+    remainings = list(remainings)
+    currents = list(currents)
+
+    for n in nexts:
+        d = dists[(current, n)]
+
+        if d >= remaining:
+            continue
+
+        flow, _ = valves[n]
+
+        remainings[i] = remaining - d - 1
+        currents[i] = n
+        new_score = score + (remaining - d - 1) * flow
+
+        scores.append(search(tuple(currents), opened.union(set([n])), tuple(remainings), new_score, []))
+
+    return max(scores)
+
+
+score_cache = {}
+score = search(('AA', ), set(), (30, ), 0, [])
+print(score)
+
+score_cache = {}
+score = search(('AA', 'AA'), set(), (26, 26), 0, ([], []))
+print(score)