This commit is contained in:
Yanick Champoux 2023-11-30 10:10:58 -05:00
parent 9adefbc796
commit 850f4f963f
4 changed files with 67 additions and 59 deletions

4
2022/16/fast.js Normal file
View File

@ -0,0 +1,4 @@
import part1, { sample, puzzleInput } from "./part1.js";
import part2 from "./part2.js";
console.log(part2(puzzleInput));

View File

@ -4,71 +4,69 @@ import u from 'updeep';
import { buildGraph } from './part1.js';
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 >)
const findMaxSteam = (tunnels,graph, minutesLeft, unopened, opened, activeSteam, peeps ) => {
console.log({ unopened, activeSteam, minutesLeft});
let next = R.sortBy(peeps, R.prop('eta'));
if( next[0].eta > minutesLeft ) {
return minutesLeft * activeSteam;
}
function finalSteam(tunnels, graph, minutesLeft, possibilities, locations, activeSteam = 0) {
const delta = next[0].eta;
const location = next[0].destination;
const base = delta * activeSteam;
minutesLeft -= delta;
activeSteam += tunnels[ next[0].destination ].flow;
opened = [ ...opened, next[0].destination ];
next = u.updateIn( '1.eta', eta => eta - delta, next );
const scores = [];
const nextToGo = locations.filter(([time]) => !time);
for( const l of locations) {
if( l[0] === 0 ) {
activeSteam += l[2];
l[2] = 0;
let nothing = true;
for ( const destination of unopened ) {
const path = bidirectional(graph, location, destination );
if( path.length > minutesLeft) {
continue;
}
nothing = false;
next = u.updateIn( '0', {
location,
eta: path.length,
destination
},next);
scores.push( findMaxSteam(tunnels,graph,minutesLeft,
unopened.filter( x => x !== destination ),
opened, activeSteam, next
) );
}
if( nothing ) {
next = u.updateIn( '0', {
location,
eta: 999,
destination: 'END',
},next);
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
)
)
scores.push( findMaxSteam(tunnels,graph,minutesLeft,
unopened,
opened, activeSteam, next
) );
}
}
}
return base + Math.max( ...scores );
return Math.max( ...scores );
}
export default (tunnels) => {
@ -80,9 +78,9 @@ export default (tunnels) => {
const graph = buildGraph(tunnels);
return finalSteam(tunnels, graph, 26, possibilities, [
[ 0, 'AA',0 ],
[ 0, 'AA',0 ]
], 0, 0)
return findMaxSteam(tunnels, graph, 26, possibilities,[],0, [
{ destination: 'AA', eta: 0 },
{ destination: 'AA', eta: 0 },
])
};

View File

@ -17,7 +17,7 @@ describe("part 1", () => {
test("sample", () => {
expect(part1(sample)).toEqual(1651);
});
test.only("solution", () => {
test("solution", () => {
const r= part1(puzzleInput);
expect(r).toBeGreaterThan(707);
expectSolution(part1(puzzleInput)).toEqual(1871);
@ -25,10 +25,10 @@ describe("part 1", () => {
});
describe.only("part 2", () => {
test("sample", () => {
test.skip("sample", () => {
expect(part2(sample)).toEqual(1707);
});
test.todo("solution", () => {
test("solution", () => {
expectSolution(part2(puzzleInput)).toEqual("TODO");
});
});

View File

@ -12,13 +12,19 @@
"license": "ISC",
"dependencies": {
"@a-robu/victor": "^2.2.2",
"@yanick/vyktor": "link:../vyktor",
"combinatorial-generators": "^1.1.2",
"debug": "^4.3.4",
"fs-extra": "^11.1.0",
"graphology": "^0.25.1",
"graphology-shortest-path": "^2.0.1",
"graphology-types": "^0.24.5",
"memoizerific": "^1.11.3",
"prettier": "^2.8.0",
"remeda": "^1.3.0",
"vite": "^4.0.1",
"vitest": "0.25.7"
"updeep": "^1.2.1",
"vite": "4.0.3",
"vitest": "0.26.3"
},
"devDependencies": {
"@vitest/ui": "^0.25.8"