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'; import { buildGraph } from './part1.js';
const findMaxSteam = (tunnels,graph) => (minutesLeft, unopened, opened, activeSteam, peeps ) => { 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 >)
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) { let nothing = true;
if( l[0] === 0 ) { for ( const destination of unopened ) {
activeSteam += l[2]; const path = bidirectional(graph, location, destination );
l[2] = 0;
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 = []; scores.push( findMaxSteam(tunnels,graph,minutesLeft,
unopened,
for ( const ntg of nextToGo ) { opened, activeSteam, next
) );
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 base + Math.max( ...scores );
}
return Math.max( ...scores );
} }
export default (tunnels) => { export default (tunnels) => {
@ -80,9 +78,9 @@ export default (tunnels) => {
const graph = buildGraph(tunnels); const graph = buildGraph(tunnels);
return finalSteam(tunnels, graph, 26, possibilities, [ return findMaxSteam(tunnels, graph, 26, possibilities,[],0, [
[ 0, 'AA',0 ], { destination: 'AA', eta: 0 },
[ 0, 'AA',0 ] { destination: 'AA', eta: 0 },
], 0, 0) ])
}; };

View File

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

View File

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