Yanick Champoux 2023-01-02 10:53:14 -05:00
parent 1a855600cf
commit 231cc7b883
1 changed files with 10 additions and 7 deletions

View File

@ -4,13 +4,12 @@ import * as R from "remeda";
import { buildGraph } from './part1.js'; import { buildGraph } from './part1.js';
function finalSteam(tunnels, graph, minutesLeft, possibilities, locations, activeSteam = 0) { function finalSteam(tunnels, graph, minutesLeft, possibilities, locations, activeSteam = 0) {
//console.log(minutesLeft, activeSteam, locations, possibilities);
if (minutesLeft <= 0) return 0; if (minutesLeft <= 0) return 0;
if (possibilities.length === 0) return minutesLeft * activeSteam; if (possibilities.length === 0) return minutesLeft * activeSteam;
const nextToGo = locations.find(([time]) => !time); const nextToGo = locations.filter(([time]) => !time);
for( const l of locations) { for( const l of locations) {
if( l[0] === 0 ) { if( l[0] === 0 ) {
@ -22,18 +21,21 @@ function finalSteam(tunnels, graph, minutesLeft, possibilities, locations, activ
let scores = []; let scores = [];
for ( const ntg of nextToGo ) {
for ( const next of possibilities ) { for ( const next of possibilities ) {
const path = bidirectional(graph, nextToGo[1], next); const path = bidirectional(graph, ntg[1], next);
//console.log(path); //console.log(path);
const locs = locations.map( x => x === nextToGo ? [ const locs = locations.map( x => x === ntg ? [
path.length, next, tunnels[next].flow path.length, next, tunnels[next].flow
] : x ); ] : x );
let time = Math.min( ...locs.map( ([time]) => time ) ); let time = Math.min( ...locs.map( ([time]) => time ) );
if (time >= minutesLeft) { if (time >= minutesLeft) {
//console.log(minutesLeft, activeSteam, locations, possibilities);
scores.push( minutesLeft * activeSteam ); scores.push( minutesLeft * activeSteam );
} }
else { else {
@ -44,13 +46,14 @@ function finalSteam(tunnels, graph, minutesLeft, possibilities, locations, activ
ts + finalSteam( ts + finalSteam(
tunnels, graph, minutesLeft - time, tunnels, graph, minutesLeft - time,
possibilities.filter( x => x !== next ), possibilities.filter( x => x !== next ),
locs.map( ([t,dest] ) => [ t -time, dest ]), locs.map( ([t,dest,s] ) => [ t -time, dest,s ]),
activeSteam activeSteam
) )
) )
} }
} }
}
return Math.max( ...scores ); return Math.max( ...scores );
} }
@ -65,8 +68,8 @@ export default (tunnels) => {
const graph = buildGraph(tunnels); const graph = buildGraph(tunnels);
return finalSteam(tunnels, graph, 26, possibilities, [ return finalSteam(tunnels, graph, 26, possibilities, [
[ 0, 'AA' ], [ 0, 'AA',0 ],
[ 0, 'AA' ] [ 0, 'AA',0 ]
], 0, 0) ], 0, 0)
}; };