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';
|
||||
|
||||
const findMaxSteam = (tunnels,graph) => (minutesLeft, unopened, opened, activeSteam, peeps ) => {
|
||||
if (minutesLeft <= 0) return 0;
|
||||
const findMaxSteam = (tunnels,graph, minutesLeft, unopened, opened, activeSteam, peeps ) => {
|
||||
|
||||
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 >)
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
if( next[0].eta > minutesLeft ) {
|
||||
return minutesLeft * activeSteam;
|
||||
}
|
||||
|
||||
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 ) {
|
||||
const path = bidirectional(graph, ntg[1], next);
|
||||
next = u.updateIn( '1.eta', eta => eta - delta, 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);
|
||||
scores.push( minutesLeft * activeSteam );
|
||||
|
||||
let nothing = true;
|
||||
for ( const destination of unopened ) {
|
||||
const path = bidirectional(graph, location, destination );
|
||||
|
||||
if( path.length > minutesLeft) {
|
||||
continue;
|
||||
}
|
||||
else {
|
||||
//console.log({totalSteam, time, activeSteam});
|
||||
let ts = time * activeSteam;
|
||||
nothing = false;
|
||||
|
||||
scores.push(
|
||||
ts + finalSteam(
|
||||
tunnels, graph, minutesLeft - time,
|
||||
possibilities.filter( x => x !== next ),
|
||||
locs.map( ([t,dest,s] ) => [ t -time, dest,s ]),
|
||||
activeSteam
|
||||
)
|
||||
)
|
||||
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);
|
||||
|
||||
scores.push( findMaxSteam(tunnels,graph,minutesLeft,
|
||||
unopened,
|
||||
opened, activeSteam, next
|
||||
) );
|
||||
}
|
||||
|
||||
return Math.max( ...scores );
|
||||
return base + 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 },
|
||||
])
|
||||
|
||||
};
|
||||
|
@ -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");
|
||||
});
|
||||
});
|
||||
|
10
package.json
10
package.json
@ -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"
|
||||
|
Loading…
Reference in New Issue
Block a user