76 lines
1.8 KiB
JavaScript
76 lines
1.8 KiB
JavaScript
import { bidirectional } from 'graphology-shortest-path';
|
|
import * as R from "remeda";
|
|
|
|
import { buildGraph } from './part1.js';
|
|
|
|
function finalSteam(tunnels, graph, minutesLeft, possibilities, locations, activeSteam = 0) {
|
|
|
|
if (minutesLeft <= 0) return 0;
|
|
|
|
if (possibilities.length === 0) return minutesLeft * activeSteam;
|
|
|
|
const nextToGo = locations.filter(([time]) => !time);
|
|
|
|
for( const l of locations) {
|
|
if( l[0] === 0 ) {
|
|
activeSteam += l[2];
|
|
l[2] = 0;
|
|
}
|
|
}
|
|
|
|
|
|
let scores = [];
|
|
|
|
for ( const ntg of nextToGo ) {
|
|
|
|
for ( const next of possibilities ) {
|
|
const path = bidirectional(graph, ntg[1], next);
|
|
|
|
//console.log(path);
|
|
|
|
const locs = locations.map( x => x === ntg ? [
|
|
path.length, next, tunnels[next].flow
|
|
] : x );
|
|
|
|
let time = Math.min( ...locs.map( ([time]) => time ) );
|
|
|
|
if (time >= minutesLeft) {
|
|
//console.log(minutesLeft, activeSteam, locations, possibilities);
|
|
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 ),
|
|
locs.map( ([t,dest,s] ) => [ t -time, dest,s ]),
|
|
activeSteam
|
|
)
|
|
)
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
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, [
|
|
[ 0, 'AA',0 ],
|
|
[ 0, 'AA',0 ]
|
|
], 0, 0)
|
|
|
|
};
|