adventofcode/2022/16/part2.js

89 lines
2.0 KiB
JavaScript
Raw Normal View History

2022-12-25 22:09:27 +00:00
import { bidirectional } from 'graphology-shortest-path';
2022-12-18 22:13:07 +00:00
import * as R from "remeda";
2023-01-02 16:02:06 +00:00
import u from 'updeep';
2022-12-18 22:13:07 +00:00
2022-12-25 22:09:27 +00:00
import { buildGraph } from './part1.js';
2022-12-18 22:13:07 +00:00
2023-01-02 16:02:06 +00:00
const findMaxSteam = (tunnels,graph) => (minutesLeft, unopened, opened, activeSteam, peeps ) => {
if (minutesLeft <= 0) return 0;
if (unopened.length === 0) return minutesLeft * activeSteam;
const next = R.first( R.sortBy(peeps, R.prop('eta')));
if( next.eta >)
}
2022-12-25 22:09:27 +00:00
function finalSteam(tunnels, graph, minutesLeft, possibilities, locations, activeSteam = 0) {
2023-01-02 15:53:14 +00:00
const nextToGo = locations.filter(([time]) => !time);
2022-12-25 22:09:27 +00:00
for( const l of locations) {
if( l[0] === 0 ) {
activeSteam += l[2];
l[2] = 0;
}
}
let scores = [];
2023-01-02 15:53:14 +00:00
for ( const ntg of nextToGo ) {
2022-12-25 22:09:27 +00:00
for ( const next of possibilities ) {
2023-01-02 15:53:14 +00:00
const path = bidirectional(graph, ntg[1], next);
2022-12-25 22:09:27 +00:00
//console.log(path);
2023-01-02 15:53:14 +00:00
const locs = locations.map( x => x === ntg ? [
2022-12-25 22:09:27 +00:00
path.length, next, tunnels[next].flow
] : x );
let time = Math.min( ...locs.map( ([time]) => time ) );
if (time >= minutesLeft) {
2023-01-02 15:53:14 +00:00
//console.log(minutesLeft, activeSteam, locations, possibilities);
2022-12-25 22:09:27 +00:00
scores.push( minutesLeft * activeSteam );
}
else {
//console.log({totalSteam, time, activeSteam});
let ts = time * activeSteam;
scores.push(
ts + finalSteam(
tunnels, graph, minutesLeft - time,
possibilities.filter( x => x !== next ),
2023-01-02 15:53:14 +00:00
locs.map( ([t,dest,s] ) => [ t -time, dest,s ]),
2022-12-25 22:09:27 +00:00
activeSteam
)
)
}
}
2023-01-02 15:53:14 +00:00
}
2022-12-25 22:09:27 +00:00
return Math.max( ...scores );
}
export default (tunnels) => {
const possibilities =
Object.keys(tunnels).filter(
k => tunnels[k].flow
);
const graph = buildGraph(tunnels);
return finalSteam(tunnels, graph, 26, possibilities, [
2023-01-02 15:53:14 +00:00
[ 0, 'AA',0 ],
[ 0, 'AA',0 ]
2022-12-25 22:09:27 +00:00
], 0, 0)
};