67 lines
1.6 KiB
JavaScript
67 lines
1.6 KiB
JavaScript
|
import * as R from "remeda";
|
||
|
import u from "updeep";
|
||
|
|
||
|
import { readFile } from "../05/part1.js";
|
||
|
|
||
|
const parseLine = (line) => {
|
||
|
const {groups} = line.match( /Valve (?<valve>..).*flow rate=(?<flow>\d+).*valves? (?<exits>.*)/ );
|
||
|
return [ groups.valve, {
|
||
|
exits: groups.exits.split(',').map( x => x.trim() ),
|
||
|
flow: groups.flow
|
||
|
} ]
|
||
|
}
|
||
|
const readInput = (...args) =>
|
||
|
R.pipe(
|
||
|
readFile(...args),
|
||
|
(lines) => lines.split("\n"),
|
||
|
R.compact, // remove last line
|
||
|
R.map(parseLine),
|
||
|
Object.fromEntries,
|
||
|
);
|
||
|
|
||
|
export const puzzleInput = readInput(import.meta.url, "input");
|
||
|
export const sample = readInput(import.meta.url, "sample");
|
||
|
|
||
|
function bestMoveFor(tunnels,minutesLeft,opened,location='AA',totalSteam=0,activeSteam=0) {
|
||
|
console.log(minutesLeft, totalSteam);
|
||
|
|
||
|
if(minutesLeft<=0) return totalSteam;
|
||
|
|
||
|
totalSteam += activeSteam;
|
||
|
|
||
|
const possibilities = [];
|
||
|
|
||
|
if(!opened.includes(location)) {
|
||
|
possibilities.push(
|
||
|
bestMoveFor(
|
||
|
tunnels,
|
||
|
minutesLeft-1,
|
||
|
[...opened, location],
|
||
|
location,
|
||
|
totalSteam,
|
||
|
activeSteam + tunnels[location].flow
|
||
|
)
|
||
|
)
|
||
|
}
|
||
|
|
||
|
for( const exit of tunnels[location].exits ) {
|
||
|
possibilities.push(
|
||
|
bestMoveFor(
|
||
|
tunnels,
|
||
|
minutesLeft-1,
|
||
|
opened,
|
||
|
exit,
|
||
|
totalSteam,
|
||
|
activeSteam,
|
||
|
)
|
||
|
)
|
||
|
}
|
||
|
|
||
|
return Math.max( ...possibilities );
|
||
|
}
|
||
|
|
||
|
|
||
|
export default (tunnels) => {
|
||
|
return bestMoveFor(tunnels,30,[]);
|
||
|
};
|