the frist valve is not always equal 'AA'

ughhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
This commit is contained in:
eleith 2022-12-20 06:18:17 -08:00
parent 721ff25926
commit 59bb9fff77
4 changed files with 485 additions and 0 deletions

229
16/README.md Normal file
View File

@ -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 1940.
--- 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?
Your puzzle answer was 2469.

59
16/input.txt Normal file
View File

@ -0,0 +1,59 @@
Valve ZT has flow rate=0; tunnels lead to valves QQ, DS
Valve JX has flow rate=22; tunnels lead to valves CI, ZH, UR
Valve EM has flow rate=0; tunnels lead to valves WH, IT
Valve AA has flow rate=0; tunnels lead to valves EQ, QD, NP, ZP, KX
Valve HW has flow rate=0; tunnels lead to valves CI, BV
Valve IK has flow rate=8; tunnels lead to valves ET, NU, ZO, XL, QD
Valve HA has flow rate=0; tunnels lead to valves WQ, LB
Valve WH has flow rate=12; tunnels lead to valves EM, LW
Valve KU has flow rate=0; tunnels lead to valves BV, CF
Valve QD has flow rate=0; tunnels lead to valves AA, IK
Valve CF has flow rate=18; tunnels lead to valves KU, JT, CM
Valve VC has flow rate=0; tunnels lead to valves AD, UY
Valve JT has flow rate=0; tunnels lead to valves CF, ZH
Valve QQ has flow rate=11; tunnel leads to valve ZT
Valve ZP has flow rate=0; tunnels lead to valves EZ, AA
Valve LI has flow rate=0; tunnels lead to valves LB, CM
Valve CI has flow rate=0; tunnels lead to valves HW, JX
Valve VK has flow rate=6; tunnels lead to valves YM, LC, HE, NU, TI
Valve WL has flow rate=20; tunnels lead to valves LW, TO
Valve TI has flow rate=0; tunnels lead to valves VK, YW
Valve NU has flow rate=0; tunnels lead to valves VK, IK
Valve DS has flow rate=9; tunnels lead to valves NP, MV, FR, ZT, YW
Valve HE has flow rate=0; tunnels lead to valves VK, EQ
Valve ZH has flow rate=0; tunnels lead to valves JT, JX
Valve TO has flow rate=0; tunnels lead to valves MT, WL
Valve CM has flow rate=0; tunnels lead to valves LI, CF
Valve WM has flow rate=14; tunnels lead to valves MO, WQ, EC, RN
Valve EZ has flow rate=16; tunnels lead to valves RT, RZ, ZP
Valve PB has flow rate=0; tunnels lead to valves YM, UY
Valve XL has flow rate=0; tunnels lead to valves IK, MS
Valve LB has flow rate=17; tunnels lead to valves LI, HA, ON, UR, AD
Valve WQ has flow rate=0; tunnels lead to valves WM, HA
Valve BV has flow rate=13; tunnels lead to valves KU, RT, HW, MO, EH
Valve RN has flow rate=0; tunnels lead to valves WM, RZ
Valve LW has flow rate=0; tunnels lead to valves WH, WL
Valve NP has flow rate=0; tunnels lead to valves AA, DS
Valve MT has flow rate=0; tunnels lead to valves TO, HG
Valve ET has flow rate=0; tunnels lead to valves IK, EC
Valve HG has flow rate=19; tunnel leads to valve MT
Valve MV has flow rate=0; tunnels lead to valves UY, DS
Valve RT has flow rate=0; tunnels lead to valves BV, EZ
Valve ON has flow rate=0; tunnels lead to valves LB, EH
Valve MO has flow rate=0; tunnels lead to valves BV, WM
Valve UY has flow rate=5; tunnels lead to valves PB, BR, MS, VC, MV
Valve UR has flow rate=0; tunnels lead to valves JX, LB
Valve YM has flow rate=0; tunnels lead to valves PB, VK
Valve RZ has flow rate=0; tunnels lead to valves RN, EZ
Valve AD has flow rate=0; tunnels lead to valves VC, LB
Valve EH has flow rate=0; tunnels lead to valves ON, BV
Valve EQ has flow rate=0; tunnels lead to valves AA, HE
Valve KX has flow rate=0; tunnels lead to valves AA, BR
Valve BR has flow rate=0; tunnels lead to valves UY, KX
Valve LC has flow rate=0; tunnels lead to valves VK, IT
Valve YW has flow rate=0; tunnels lead to valves TI, DS
Valve EC has flow rate=0; tunnels lead to valves ET, WM
Valve IT has flow rate=10; tunnels lead to valves LC, EM
Valve MS has flow rate=0; tunnels lead to valves UY, XL
Valve FR has flow rate=0; tunnels lead to valves DS, ZO
Valve ZO has flow rate=0; tunnels lead to valves FR, IK

10
16/sample-input.txt Normal file
View File

@ -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

187
16/src/index.ts Normal file
View File

@ -0,0 +1,187 @@
import { readFileSync } from 'fs'
type Valve = {
name: string
pressure: number
tunnels: Valve[]
timeTo: { [key: string]: number }
}
const parser = /Valve (.+) has flow rate=(\d+); tunnels? leads? to valves? (.+)/
function parseValveData(lines: string[]): Valve[] {
const tunnelMap: { [key: string]: string[] } = {}
const valves = lines.map((line) => {
const matcher = line.match(parser)
if (matcher) {
const name = matcher[1]
const pressure = Number(matcher[2])
tunnelMap[name] = matcher[3].split(', ')
return {
name,
pressure,
tunnels: [],
timeTo: {},
} as Valve
}
throw new Error(`Could not parse line: ${line}`)
})
valves.forEach((valve) => {
valve.tunnels = tunnelMap[valve.name].map((name) => {
const tunnel = valves.find((v) => v.name === name)
if (tunnel) {
return tunnel
}
throw new Error(`Could not find tunnel ${name}`)
})
})
valves.forEach((valve) => {
valves
.filter((x) => x.pressure > 0)
.forEach((otherValve) => {
valve.timeTo[otherValve.name] = fastestTimeTo(valve, otherValve)
})
})
return valves
}
function getPressure(valve: Valve, minutes: number): number {
return valve.pressure * (minutes > 0 ? minutes : 0)
}
function fastestTimeTo(start: Valve, end: Valve): number {
let count = 0
const queue: [Valve, number][] = [[start, count]]
const visited: string[] = [start.name]
while (queue.length > 0) {
const [valve, depth] = queue.shift() as [Valve, number]
if (valve.name === end.name) {
count = depth
break
}
valve.tunnels
.filter((v) => !visited.includes(v.name))
.forEach((neighbor) => {
queue.push([neighbor, depth + 1])
visited.push(neighbor.name)
})
}
return count
}
function maxPressureFor(
pressuredValves: Valve[],
valve: Valve,
time: number,
pressure = 0,
callback?: (path: string[], pressure: number) => void,
pathSeen: string[] = []
): number {
if (time <= 1) {
return pressure
}
if (pressuredValves.length === 0) {
return pressure
}
return pressuredValves
.map((valvePressured) => {
let updatedTime = time
let updatedPressure = pressure
let updatedPath = pathSeen
updatedTime = time - valve.timeTo[valvePressured.name] - 1
updatedPressure = pressure + getPressure(valvePressured, updatedTime)
updatedPath = [...pathSeen, valvePressured.name]
callback?.(updatedPath, updatedPressure)
return maxPressureFor(
pressuredValves.filter((v) => v.name !== valvePressured.name),
valvePressured,
updatedTime,
updatedPressure,
callback,
updatedPath
)
})
.sort((a, b) => a - b)
.pop() as number
}
function findBestTwoDisjointPaths(allPathScores: { [key: string]: number }): number {
const orderedPaths = Object.entries(allPathScores)
.sort(([, pressure1], [, pressure2]) => pressure2 - pressure1)
.map(([key]) => key.split('-'))
const best = orderedPaths.reduce((scores, path) => {
orderedPaths.find((path2) => {
if (path2.every((x) => !path.includes(x))) {
const pathKey = path.join('-')
const path2Key = path2.join('-')
if (allPathScores[path2Key] && allPathScores[pathKey]) {
scores.push(Math.max(allPathScores[path2Key] + allPathScores[pathKey]))
return true
}
}
return false
})
return scores
}, [] as number[])
return best.sort((a, b) => b - a).shift() as number
}
function findMaxPressure(valves: Valve[], minutes: number): number {
const hasPressure = valves.filter((v) => v.pressure > 0)
const valveAA = valves.find((v) => v.name === 'AA') as Valve
return maxPressureFor(hasPressure, valveAA, minutes)
}
function findMaxPressureWithHelp(valves: Valve[], minutes: number): number {
const cache: { [key: string]: number } = {}
const caching = (path: string[], pressure: number) => {
const key = path.sort().join('-')
const getMemo = cache[key]
cache[key] = getMemo ? Math.max(pressure, getMemo) : pressure
}
const hasPressure = valves.filter((v) => v.pressure > 0)
const valveAA = valves.find((v) => v.name === 'AA') as Valve
maxPressureFor(hasPressure, valveAA, minutes, 0, caching)
return findBestTwoDisjointPaths(cache)
}
function solvePart1(lines: string[]): number {
const valves = parseValveData(lines)
return findMaxPressure(valves, 30)
}
function solvePart2(lines: string[]): number {
const valves = parseValveData(lines)
return findMaxPressureWithHelp(valves, 26)
}
function run() {
const input = process.argv.slice(2)[0]
const file = readFileSync(input, 'utf8')
const lines = file.split('\n').slice(0, -1)
const solution1 = solvePart1(lines)
console.log(`part1: ${solution1}`)
const solution2 = solvePart2(lines)
console.log(`part2: ${solution2}`)
}
run()