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 1641
.
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.
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 2261
.
DFS search. Nothing out of the ordinary:
interface Valve {
name: string;
flowRate: number;
leadToValves: string[];
}
export interface Simulation {
name: string;
valves: Map<string, Valve>;
}
function findMaxPressureRelease({ valves }: Simulation) {
const visited = new Map<string, { released: number; opened: Set<string> }>();
const helper = (
valveName: string,
remainingTime: number,
openedValve: Set<string>
): { released: number; opened: Set<string> } => {
if (remainingTime <= 0) return { released: 0, opened: openedValve };
const key =
valveName +
remainingTime.toString() +
[...openedValve].sort((a, b) => a.localeCompare(b)).join(',');
if (visited.has(key)) return visited.get(key)!;
const valve = valves.get(valveName)!;
let maxFlow = { released: -Infinity, opened: new Set<string>() };
// Consider all subsequent valves.
for (const nextValve of valve.leadToValves) {
const options = [helper(nextValve, remainingTime - 1, openedValve)];
if (valve.flowRate > 0 && !openedValve.has(valveName)) {
const ans = helper(
nextValve,
remainingTime - 2,
new Set(openedValve).add(valveName)
);
options.push({
...ans,
released: ans.released + valve.flowRate * (remainingTime - 1),
});
}
for (const option of options) {
if (option.released > maxFlow.released) maxFlow = option;
}
}
visited.set(key, maxFlow);
return maxFlow;
};
const ans = helper('AA', 30, new Set(['AA']));
return ans;
}
You need to transform the problem into a smaller search space. Instead of searching the entire grid of flows,
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
you search an adjaceny matrix instead. I.e.,
AA BB CC DD EE FF GG HH II JJ
AA 0 1 2 1 2 3 4 5 1 2
BB 1 0 1 2 3 4 5 6 2 3
CC 2 1 0 1 2 3 4 5 3 4
DD 1 2 1 0 1 2 3 4 2 3
EE 2 3 2 1 0 1 2 3 3 4
FF 3 4 3 2 1 0 1 2 4 5
GG 4 5 4 3 2 1 0 1 5 6
HH 5 6 5 4 3 2 1 0 6 7
II 1 2 3 2 3 4 5 6 0 1
JJ 2 3 4 3 4 5 6 7 1 0
Which shows how long it takes to go from row to column. It's a symmetric matrix.
E.g., takes 5 minutes to go from AA
to HH
.
You can reduce the adjacency matrix further by eliminating nodes that have 0
flow rate, because why would you go there? Don't remove AA
even though it has
0 flow rate because that's the starting node.
AA BB CC DD EE HH JJ
AA 0 1 2 1 2 5 2
BB 1 0 1 2 3 6 3
CC 2 1 0 1 2 5 4
DD 1 2 1 0 1 4 3
EE 2 3 2 1 0 3 4
HH 5 6 5 4 3 0 7
JJ 2 3 4 3 4 7 0
The code to get here so far uses Floyd–Warshall algorithm. See
- OneNote -> CompSci -> Coding -> Graph Representation -> Floyd–Warshall.
interface AdjacencyMatrix {
[fromValve: string]: { [toValve: string]: number };
}
interface AdjSim {
adj: AdjacencyMatrix;
sim: Simulation;
}
function getAdjacencyMatrix(sim: Simulation) {
const { valves } = sim;
const response: AdjacencyMatrix = {};
const allValves = new Set<string>();
for (const [name, valve] of valves) {
response[name] = { [name]: 0 };
allValves.add(name);
for (const toValve of valve.leadToValves) {
response[name][toValve] = 1;
allValves.add(toValve);
}
}
for (const i of allValves) {
for (const j of allValves) {
response[i] ??= {};
response[i][j] ??= Infinity;
}
}
for (const k of allValves) {
for (const i of allValves) {
for (const j of allValves) {
response[i][j] = Math.min(
response[i][j],
response[i][k] + response[k][j]
);
}
}
}
return { adj: response, sim };
}
function pruneZeroFlow(params: AdjSim) {
const ignore = 'AA';
const {
adj,
sim: { valves },
} = params;
const removed = new Set<string>();
for (const valve of Object.keys(adj)) {
if (valves.get(valve)!.flowRate === 0 && valve !== ignore) {
delete adj[valve];
removed.add(valve);
}
}
for (const i of Object.keys(adj)) {
for (const j of Object.keys(adj[i])) {
if (removed.has(j)) delete adj[i][j];
}
}
return params;
}
Now we do the backtracking DFS part. This allowed me to keep a single set that I
could add/delete from. A map of visited
for memoization is still valuable for
backtracking. For my input, even after removing all the zero flow nodes, without
a map, it would've done 9 million iterations; with a map it did only 2 million.
export function findMaxPressureRelease2(params: AdjSim) {
const {
adj,
sim: { valves },
} = params;
const visited = new Map<string, number>();
const helper = (
atValve: string,
remainingTime: number,
openedValve: Set<string>,
runAgain: boolean
): number => {
if (remainingTime <= 0) return 0;
const key =
atValve +
remainingTime.toString() +
[...openedValve].sort((a, b) => a.localeCompare(b)).join(',') +
runAgain;
if (visited.has(key)) return visited.get(key)!;
let max = 0;
// Figure out YOUR max pressure release.
for (const toValve of Object.keys(adj[atValve])) {
if (openedValve.has(toValve)) continue;
const newRemaining = remainingTime - adj[atValve][toValve] - 1;
if (newRemaining <= 0) continue;
openedValve.add(toValve);
const flow = valves.get(toValve)!.flowRate;
const yourReleasedPressure =
helper(toValve, newRemaining, openedValve, runAgain) +
flow * newRemaining;
openedValve.delete(toValve);
max = Math.max(max, yourReleasedPressure);
}
// Figure out the elephant's released pressure.
const elephantReleasedPressure = runAgain
? helper('AA', 26, openedValve, false)
: 0;
// Whoever (you or the elephant) does it best at this depth in the DFS wins
// the max.
max = Math.max(max, elephantReleasedPressure);
visited.set(key, max);
return max;
};
const ans = helper('AA', 26, new Set(['AA']), true);
return ans;
}