4 changed files with 388 additions and 0 deletions
@ -0,0 +1,229 @@ |
|||
--- Day 16: Proboscidea Volcanium --- |
|||
|
|||
The sensors have led you to the origin of the distress signal: yet another handheld device, just like the one the Elves gave you. However, you don't see any Elves around; instead, the device is surrounded by elephants! They must have gotten lost in these tunnels, and one of the elephants apparently figured out how to turn on the distress signal. |
|||
|
|||
The ground rumbles again, much stronger this time. What kind of cave is this, exactly? You scan the cave with your handheld device; it reports mostly igneous rock, some ash, pockets of pressurized gas, magma... this isn't just a cave, it's a volcano! |
|||
|
|||
You need to get the elephants out of here, quickly. Your device estimates that you have 30 minutes before the volcano erupts, so you don't have time to go back out the way you came in. |
|||
|
|||
You scan the cave for other options and discover a network of pipes and pressure-release valves. You aren't sure how such a system got into a volcano, but you don't have time to complain; your device produces a report (your puzzle input) of each valve's flow rate if it were opened (in pressure per minute) and the tunnels you could use to move between the valves. |
|||
|
|||
There's even a valve in the room you and the elephants are currently standing in labeled AA. You estimate it will take you one minute to open a single valve and one minute to follow any tunnel from one valve to another. What is the most pressure you could release? |
|||
|
|||
For example, suppose you had the following scan output: |
|||
|
|||
Valve AA has flow rate=0; tunnels lead to valves DD, II, BB |
|||
Valve BB has flow rate=13; tunnels lead to valves CC, AA |
|||
Valve CC has flow rate=2; tunnels lead to valves DD, BB |
|||
Valve DD has flow rate=20; tunnels lead to valves CC, AA, EE |
|||
Valve EE has flow rate=3; tunnels lead to valves FF, DD |
|||
Valve FF has flow rate=0; tunnels lead to valves EE, GG |
|||
Valve GG has flow rate=0; tunnels lead to valves FF, HH |
|||
Valve HH has flow rate=22; tunnel leads to valve GG |
|||
Valve II has flow rate=0; tunnels lead to valves AA, JJ |
|||
Valve JJ has flow rate=21; tunnel leads to valve II |
|||
|
|||
All of the valves begin closed. You start at valve AA, but it must be damaged or jammed or something: its flow rate is 0, so there's no point in opening it. However, you could spend one minute moving to valve BB and another minute opening it; doing so would release pressure during the remaining 28 minutes at a flow rate of 13, a total eventual pressure release of 28 * 13 = 364. Then, you could spend your third minute moving to valve CC and your fourth minute opening it, providing an additional 26 minutes of eventual pressure release at a flow rate of 2, or 52 total pressure released by valve CC. |
|||
|
|||
Making your way through the tunnels like this, you could probably open many or all of the valves by the time 30 minutes have elapsed. However, you need to release as much pressure as possible, so you'll need to be methodical. Instead, consider this approach: |
|||
|
|||
== Minute 1 == |
|||
No valves are open. |
|||
You move to valve DD. |
|||
|
|||
== Minute 2 == |
|||
No valves are open. |
|||
You open valve DD. |
|||
|
|||
== Minute 3 == |
|||
Valve DD is open, releasing 20 pressure. |
|||
You move to valve CC. |
|||
|
|||
== Minute 4 == |
|||
Valve DD is open, releasing 20 pressure. |
|||
You move to valve BB. |
|||
|
|||
== Minute 5 == |
|||
Valve DD is open, releasing 20 pressure. |
|||
You open valve BB. |
|||
|
|||
== Minute 6 == |
|||
Valves BB and DD are open, releasing 33 pressure. |
|||
You move to valve AA. |
|||
|
|||
== Minute 7 == |
|||
Valves BB and DD are open, releasing 33 pressure. |
|||
You move to valve II. |
|||
|
|||
== Minute 8 == |
|||
Valves BB and DD are open, releasing 33 pressure. |
|||
You move to valve JJ. |
|||
|
|||
== Minute 9 == |
|||
Valves BB and DD are open, releasing 33 pressure. |
|||
You open valve JJ. |
|||
|
|||
== Minute 10 == |
|||
Valves BB, DD, and JJ are open, releasing 54 pressure. |
|||
You move to valve II. |
|||
|
|||
== Minute 11 == |
|||
Valves BB, DD, and JJ are open, releasing 54 pressure. |
|||
You move to valve AA. |
|||
|
|||
== Minute 12 == |
|||
Valves BB, DD, and JJ are open, releasing 54 pressure. |
|||
You move to valve DD. |
|||
|
|||
== Minute 13 == |
|||
Valves BB, DD, and JJ are open, releasing 54 pressure. |
|||
You move to valve EE. |
|||
|
|||
== Minute 14 == |
|||
Valves BB, DD, and JJ are open, releasing 54 pressure. |
|||
You move to valve FF. |
|||
|
|||
== Minute 15 == |
|||
Valves BB, DD, and JJ are open, releasing 54 pressure. |
|||
You move to valve GG. |
|||
|
|||
== Minute 16 == |
|||
Valves BB, DD, and JJ are open, releasing 54 pressure. |
|||
You move to valve HH. |
|||
|
|||
== Minute 17 == |
|||
Valves BB, DD, and JJ are open, releasing 54 pressure. |
|||
You open valve HH. |
|||
|
|||
== Minute 18 == |
|||
Valves BB, DD, HH, and JJ are open, releasing 76 pressure. |
|||
You move to valve GG. |
|||
|
|||
== Minute 19 == |
|||
Valves BB, DD, HH, and JJ are open, releasing 76 pressure. |
|||
You move to valve FF. |
|||
|
|||
== Minute 20 == |
|||
Valves BB, DD, HH, and JJ are open, releasing 76 pressure. |
|||
You move to valve EE. |
|||
|
|||
== Minute 21 == |
|||
Valves BB, DD, HH, and JJ are open, releasing 76 pressure. |
|||
You open valve EE. |
|||
|
|||
== Minute 22 == |
|||
Valves BB, DD, EE, HH, and JJ are open, releasing 79 pressure. |
|||
You move to valve DD. |
|||
|
|||
== Minute 23 == |
|||
Valves BB, DD, EE, HH, and JJ are open, releasing 79 pressure. |
|||
You move to valve CC. |
|||
|
|||
== Minute 24 == |
|||
Valves BB, DD, EE, HH, and JJ are open, releasing 79 pressure. |
|||
You open valve CC. |
|||
|
|||
== Minute 25 == |
|||
Valves BB, CC, DD, EE, HH, and JJ are open, releasing 81 pressure. |
|||
|
|||
== Minute 26 == |
|||
Valves BB, CC, DD, EE, HH, and JJ are open, releasing 81 pressure. |
|||
|
|||
== Minute 27 == |
|||
Valves BB, CC, DD, EE, HH, and JJ are open, releasing 81 pressure. |
|||
|
|||
== Minute 28 == |
|||
Valves BB, CC, DD, EE, HH, and JJ are open, releasing 81 pressure. |
|||
|
|||
== Minute 29 == |
|||
Valves BB, CC, DD, EE, HH, and JJ are open, releasing 81 pressure. |
|||
|
|||
== Minute 30 == |
|||
Valves BB, CC, DD, EE, HH, and JJ are open, releasing 81 pressure. |
|||
|
|||
This approach lets you release the most pressure possible in 30 minutes with this valve layout, 1651. |
|||
|
|||
Work out the steps to release the most pressure in 30 minutes. What is the most pressure you can release? |
|||
|
|||
Your puzzle answer was 2330. |
|||
|
|||
The first half of this puzzle is complete! It provides one gold star: * |
|||
--- Part Two --- |
|||
|
|||
You're worried that even with an optimal approach, the pressure released won't be enough. What if you got one of the elephants to help you? |
|||
|
|||
It would take you 4 minutes to teach an elephant how to open the right valves in the right order, leaving you with only 26 minutes to actually execute your plan. Would having two of you working together be better, even if it means having less time? (Assume that you teach the elephant before opening any valves yourself, giving you both the same full 26 minutes.) |
|||
|
|||
In the example above, you could teach the elephant to help you as follows: |
|||
|
|||
== Minute 1 == |
|||
No valves are open. |
|||
You move to valve II. |
|||
The elephant moves to valve DD. |
|||
|
|||
== Minute 2 == |
|||
No valves are open. |
|||
You move to valve JJ. |
|||
The elephant opens valve DD. |
|||
|
|||
== Minute 3 == |
|||
Valve DD is open, releasing 20 pressure. |
|||
You open valve JJ. |
|||
The elephant moves to valve EE. |
|||
|
|||
== Minute 4 == |
|||
Valves DD and JJ are open, releasing 41 pressure. |
|||
You move to valve II. |
|||
The elephant moves to valve FF. |
|||
|
|||
== Minute 5 == |
|||
Valves DD and JJ are open, releasing 41 pressure. |
|||
You move to valve AA. |
|||
The elephant moves to valve GG. |
|||
|
|||
== Minute 6 == |
|||
Valves DD and JJ are open, releasing 41 pressure. |
|||
You move to valve BB. |
|||
The elephant moves to valve HH. |
|||
|
|||
== Minute 7 == |
|||
Valves DD and JJ are open, releasing 41 pressure. |
|||
You open valve BB. |
|||
The elephant opens valve HH. |
|||
|
|||
== Minute 8 == |
|||
Valves BB, DD, HH, and JJ are open, releasing 76 pressure. |
|||
You move to valve CC. |
|||
The elephant moves to valve GG. |
|||
|
|||
== Minute 9 == |
|||
Valves BB, DD, HH, and JJ are open, releasing 76 pressure. |
|||
You open valve CC. |
|||
The elephant moves to valve FF. |
|||
|
|||
== Minute 10 == |
|||
Valves BB, CC, DD, HH, and JJ are open, releasing 78 pressure. |
|||
The elephant moves to valve EE. |
|||
|
|||
== Minute 11 == |
|||
Valves BB, CC, DD, HH, and JJ are open, releasing 78 pressure. |
|||
The elephant opens valve EE. |
|||
|
|||
(At this point, all valves are open.) |
|||
|
|||
== Minute 12 == |
|||
Valves BB, CC, DD, EE, HH, and JJ are open, releasing 81 pressure. |
|||
|
|||
... |
|||
|
|||
== Minute 20 == |
|||
Valves BB, CC, DD, EE, HH, and JJ are open, releasing 81 pressure. |
|||
|
|||
... |
|||
|
|||
== Minute 26 == |
|||
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? |
@ -0,0 +1,92 @@ |
|||
#!/usr/bin/env python3 |
|||
|
|||
import sys, re |
|||
from copy import deepcopy |
|||
|
|||
|
|||
VALVES = {'V': [], 'E': [], 'w': {}} |
|||
|
|||
|
|||
def parse(line): |
|||
g = re.compile(r"Valve (?P<valve>\w{2}) has flow rate=(?P<fr>\d+); tunnel(s)? lead(s)? to valve(s)? (?P<tunnel>(\w{2}(, )?)*)").search(line) |
|||
vc = g.group('valve') + 'C' |
|||
vo = g.group('valve') + 'O' |
|||
es = g.group('tunnel').split(', ') |
|||
VALVES['V'] += [vc,vo] |
|||
VALVES['E'].append((vc,vo)) |
|||
for e in es: |
|||
VALVES['E'].append((vo,e+'C')) |
|||
VALVES['E'].append((vc,e+'C')) |
|||
VALVES['w'][(vo,e+'C')] = 0 |
|||
VALVES['w'][(vc,e+'C')] = 0 |
|||
VALVES['w'][(vc,vo)] = int(g.group('fr')) |
|||
|
|||
def Init(G,s,k): |
|||
d = {} |
|||
o = {} |
|||
for v in G['V']: |
|||
d[v] = [-1] * k |
|||
o[v] = [[]] * k |
|||
d[s] = [0] * k |
|||
return d, o |
|||
|
|||
def anti_relax(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] |
|||
|
|||
def find_path(G,s,k): |
|||
d,o = Init(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(u,v,G['w'],i,di,k,o) |
|||
d = deepcopy(di) |
|||
return max(map(max,d.values())) |
|||
|
|||
def Init_double(G,s,k): |
|||
d = {} |
|||
o = {} |
|||
for v in G['V']: |
|||
d[v] = [-1] * k |
|||
o[v] = [[]] * k |
|||
d[s] = [0] * k |
|||
return d, o |
|||
|
|||
def anti_relax_double(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] |
|||
|
|||
def find_double_path(G,s,k): |
|||
d,o = Init_double(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) |
|||
d = deepcopy(di) |
|||
return max(map(max,d.values())) |
|||
|
|||
|
|||
if __name__ == '__main__': |
|||
[parse(line.strip('\n')) for line in open(sys.argv[1])] |
|||
|
|||
m = find_path(VALVES,'AAC',29) |
|||
|
|||
# start (s): AA, length (k): 30, |
|||
|
|||
# challenge 1 |
|||
res1 = str(m) |
|||
print("challenge 1:" + "\n" + res1 + "\n") |
|||
|
|||
# challenge 2 |
|||
m = find_double_path(VALVES,'AAC',25) |
|||
# missing: magic! |
|||
print(m) |
|||
res2 = "" |
|||
print("challenge 2:" + "\n" + res2 + "\n") |
|||
|
@ -0,0 +1,10 @@ |
|||
Valve AA has flow rate=0; tunnels lead to valves DD, II, BB |
|||
Valve BB has flow rate=13; tunnels lead to valves CC, AA |
|||
Valve CC has flow rate=2; tunnels lead to valves DD, BB |
|||
Valve DD has flow rate=20; tunnels lead to valves CC, AA, EE |
|||
Valve EE has flow rate=3; tunnels lead to valves FF, DD |
|||
Valve FF has flow rate=0; tunnels lead to valves EE, GG |
|||
Valve GG has flow rate=0; tunnels lead to valves FF, HH |
|||
Valve HH has flow rate=22; tunnel leads to valve GG |
|||
Valve II has flow rate=0; tunnels lead to valves AA, JJ |
|||
Valve JJ has flow rate=21; tunnel leads to valve II |
@ -0,0 +1,57 @@ |
|||
Valve RU has flow rate=0; tunnels lead to valves YH, ID |
|||
Valve QK has flow rate=24; tunnels lead to valves PQ, PP |
|||
Valve RP has flow rate=11; tunnels lead to valves RM, BA, RI, EM |
|||
Valve BX has flow rate=0; tunnels lead to valves ZX, VK |
|||
Valve JL has flow rate=0; tunnels lead to valves ID, LC |
|||
Valve DC has flow rate=25; tunnel leads to valve ST |
|||
Valve HX has flow rate=0; tunnels lead to valves DH, FE |
|||
Valve KJ has flow rate=0; tunnels lead to valves ZK, XN |
|||
Valve EM has flow rate=0; tunnels lead to valves AW, RP |
|||
Valve XN has flow rate=7; tunnels lead to valves LH, KJ, KU, AO |
|||
Valve DH has flow rate=9; tunnels lead to valves SY, CC, QL, LH, HX |
|||
Valve LH has flow rate=0; tunnels lead to valves XN, DH |
|||
Valve PP has flow rate=0; tunnels lead to valves QK, TA |
|||
Valve AO has flow rate=0; tunnels lead to valves AA, XN |
|||
Valve SY has flow rate=0; tunnels lead to valves DH, AA |
|||
Valve MZ has flow rate=0; tunnels lead to valves JT, PF |
|||
Valve AA has flow rate=0; tunnels lead to valves JN, UN, WG, SY, AO |
|||
Valve RM has flow rate=0; tunnels lead to valves XL, RP |
|||
Valve BA has flow rate=0; tunnels lead to valves RP, YP |
|||
Valve AD has flow rate=12; tunnels lead to valves LK, ZX, AW |
|||
Valve ZN has flow rate=0; tunnels lead to valves EQ, HL |
|||
Valve EX has flow rate=18; tunnel leads to valve RB |
|||
Valve CR has flow rate=0; tunnels lead to valves TA, ST |
|||
Valve WG has flow rate=0; tunnels lead to valves AA, TA |
|||
Valve UN has flow rate=0; tunnels lead to valves WK, AA |
|||
Valve VE has flow rate=0; tunnels lead to valves JA, KW |
|||
Valve JA has flow rate=19; tunnels lead to valves PQ, VE |
|||
Valve AW has flow rate=0; tunnels lead to valves AD, EM |
|||
Valve XL has flow rate=0; tunnels lead to valves RM, PF |
|||
Valve OD has flow rate=0; tunnels lead to valves VK, RI |
|||
Valve FE has flow rate=0; tunnels lead to valves JT, HX |
|||
Valve PQ has flow rate=0; tunnels lead to valves JA, QK |
|||
Valve RB has flow rate=0; tunnels lead to valves CC, EX |
|||
Valve JT has flow rate=3; tunnels lead to valves RF, MZ, ZK, FE, DD |
|||
Valve YP has flow rate=0; tunnels lead to valves ID, BA |
|||
Valve ID has flow rate=14; tunnels lead to valves JL, RU, YP |
|||
Valve YH has flow rate=0; tunnels lead to valves RU, VK |
|||
Valve TA has flow rate=21; tunnels lead to valves WG, KU, PP, RF, CR |
|||
Valve LK has flow rate=0; tunnels lead to valves PF, AD |
|||
Valve DD has flow rate=0; tunnels lead to valves JN, JT |
|||
Valve HL has flow rate=0; tunnels lead to valves ZN, DW |
|||
Valve VK has flow rate=22; tunnels lead to valves OD, KW, BX, YH |
|||
Valve RF has flow rate=0; tunnels lead to valves JT, TA |
|||
Valve CC has flow rate=0; tunnels lead to valves RB, DH |
|||
Valve KW has flow rate=0; tunnels lead to valves VE, VK |
|||
Valve PF has flow rate=10; tunnels lead to valves WK, MZ, QL, XL, LK |
|||
Valve ZX has flow rate=0; tunnels lead to valves AD, BX |
|||
Valve JN has flow rate=0; tunnels lead to valves DD, AA |
|||
Valve ST has flow rate=0; tunnels lead to valves CR, DC |
|||
Valve WK has flow rate=0; tunnels lead to valves PF, UN |
|||
Valve DW has flow rate=13; tunnels lead to valves LC, HL |
|||
Valve ZK has flow rate=0; tunnels lead to valves KJ, JT |
|||
Valve QL has flow rate=0; tunnels lead to valves DH, PF |
|||
Valve RI has flow rate=0; tunnels lead to valves OD, RP |
|||
Valve EQ has flow rate=23; tunnel leads to valve ZN |
|||
Valve LC has flow rate=0; tunnels lead to valves JL, DW |
|||
Valve KU has flow rate=0; tunnels lead to valves XN, TA |
Loading…
Reference in new issue