wip
This commit is contained in:
parent
9adefbc796
commit
850f4f963f
4
2022/16/fast.js
Normal file
4
2022/16/fast.js
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
import part1, { sample, puzzleInput } from "./part1.js";
|
||||||
|
import part2 from "./part2.js";
|
||||||
|
|
||||||
|
console.log(part2(puzzleInput));
|
100
2022/16/part2.js
100
2022/16/part2.js
@ -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;
|
console.log({ unopened, activeSteam, minutesLeft});
|
||||||
|
|
||||||
const next = R.first( R.sortBy(peeps, R.prop('eta')));
|
let next = R.sortBy(peeps, R.prop('eta'));
|
||||||
|
|
||||||
if( next.eta >)
|
if( next[0].eta > minutesLeft ) {
|
||||||
|
return minutesLeft * activeSteam;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
function finalSteam(tunnels, graph, minutesLeft, possibilities, locations, activeSteam = 0) {
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const nextToGo = locations.filter(([time]) => !time);
|
|
||||||
|
|
||||||
for( const l of locations) {
|
|
||||||
if( l[0] === 0 ) {
|
|
||||||
activeSteam += l[2];
|
|
||||||
l[2] = 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const delta = next[0].eta;
|
||||||
|
const location = next[0].destination;
|
||||||
|
|
||||||
let scores = [];
|
const base = delta * activeSteam;
|
||||||
|
minutesLeft -= delta;
|
||||||
|
|
||||||
for ( const ntg of nextToGo ) {
|
activeSteam += tunnels[ next[0].destination ].flow;
|
||||||
|
opened = [ ...opened, next[0].destination ];
|
||||||
|
|
||||||
for ( const next of possibilities ) {
|
next = u.updateIn( '1.eta', eta => eta - delta, next );
|
||||||
const path = bidirectional(graph, ntg[1], next);
|
|
||||||
|
|
||||||
//console.log(path);
|
const scores = [];
|
||||||
|
|
||||||
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);
|
let nothing = true;
|
||||||
scores.push( minutesLeft * activeSteam );
|
for ( const destination of unopened ) {
|
||||||
|
const path = bidirectional(graph, location, destination );
|
||||||
|
|
||||||
|
if( path.length > minutesLeft) {
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
else {
|
nothing = false;
|
||||||
//console.log({totalSteam, time, activeSteam});
|
|
||||||
let ts = time * activeSteam;
|
|
||||||
|
|
||||||
scores.push(
|
next = u.updateIn( '0', {
|
||||||
ts + finalSteam(
|
location,
|
||||||
tunnels, graph, minutesLeft - time,
|
eta: path.length,
|
||||||
possibilities.filter( x => x !== next ),
|
destination
|
||||||
locs.map( ([t,dest,s] ) => [ t -time, dest,s ]),
|
},next);
|
||||||
activeSteam
|
|
||||||
)
|
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);
|
||||||
|
|
||||||
|
scores.push( findMaxSteam(tunnels,graph,minutesLeft,
|
||||||
|
unopened,
|
||||||
|
opened, activeSteam, next
|
||||||
|
) );
|
||||||
}
|
}
|
||||||
|
|
||||||
return Math.max( ...scores );
|
return base + 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)
|
])
|
||||||
|
|
||||||
};
|
};
|
||||||
|
@ -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");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
10
package.json
10
package.json
@ -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"
|
||||||
|
Loading…
Reference in New Issue
Block a user