diff --git a/2022/16/fast.js b/2022/16/fast.js new file mode 100644 index 0000000..5357596 --- /dev/null +++ b/2022/16/fast.js @@ -0,0 +1,4 @@ +import part1, { sample, puzzleInput } from "./part1.js"; +import part2 from "./part2.js"; + +console.log(part2(puzzleInput)); diff --git a/2022/16/part2.js b/2022/16/part2.js index 441d100..1c649cb 100644 --- a/2022/16/part2.js +++ b/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 >) + if( next[0].eta > minutesLeft ) { + return minutesLeft * activeSteam; + } + + 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 = []; -} + let nothing = true; + for ( const destination of unopened ) { + const path = bidirectional(graph, location, destination ); -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( 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 }, + ]) }; diff --git a/2022/16/test.js b/2022/16/test.js index 14f8d9d..44c122e 100644 --- a/2022/16/test.js +++ b/2022/16/test.js @@ -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"); }); }); diff --git a/package.json b/package.json index 86b09f3..b49046c 100644 --- a/package.json +++ b/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"