This commit is contained in:
Yanick Champoux 2022-12-12 20:52:48 -05:00
parent bcdbd3d805
commit c4f0e4f606
2 changed files with 33 additions and 33 deletions

View File

@ -27,6 +27,17 @@ export const findStart = (topoMap) => {
return V(x,y); return V(x,y);
}; };
export const findEnd = (topoMap) => {
let y;
let x = R.findIndex(topoMap, (row) => {
let z = row.indexOf("{");
if (z === -1) return false;
y = z;
return true;
});
return V(x,y);
};
const directions = [ [0,1], [0,-1],[1,0],[-1,0] ].map( const directions = [ [0,1], [0,-1],[1,0],[-1,0] ].map(
d => V(...d) d => V(...d)
); );
@ -42,52 +53,42 @@ const isReachable = (topoMap,pos) => {
const baseline = topoMap[pos.x][pos.y].charCodeAt(0); const baseline = topoMap[pos.x][pos.y].charCodeAt(0);
return (next) => { return (next) => {
return [0,1].includes( return topoMap[next.x][next.y].charCodeAt(0) - baseline <= 1;
topoMap[next.x][next.y].charCodeAt(0) - baseline
)
} }
} }
function findShortestPath(topoMap) { function findShortestPath(topoMap) {
const initial = findStart(topoMap); const initial = findStart(topoMap);
initial.steps = 0; initial.steps = 0;
initial.beenThere = []; const final = findEnd(topoMap);
const potentials = [ initial ]; const potentials = [ initial ];
let bestSoFar; const beenThere = topoMap.map(
line => line.map( () => 9999 )
);
const oom = outOfMap(topoMap.length,topoMap[0].length ); const oom = outOfMap(topoMap.length,topoMap[0].length );
let failsafe = 100000; let failsafe = 10; //topoMap.length*topoMap[0].length ;
while( potentials.length > 0 ) { while( potentials.length > 0 ) {
// if(! failsafe--) return; // if(! failsafe--) return;
const pos = potentials.pop(); // depth-first const pos = potentials.shift(); // depth-first
//console.log(pos.steps, bestSoFar);
if( bestSoFar && (bestSoFar <= pos.steps) ) continue; if( beenThere[pos.x][pos.y] <= pos.steps ) continue;
beenThere[pos.x][pos.y] = pos.steps;
if( topoMap[pos.x][pos.y] === '{' ) {
bestSoFar = pos.steps;
continue;
}
const next = directions.map( const next = directions.map(
d => d.clone().add(pos) d => d.clone().add(pos)
).filter( R.isNot(oom )) ).filter( R.isNot(oom ))
.filter( isReachable(topoMap,pos) ) .filter( isReachable(topoMap,pos) );
.filter( next => !pos.beenThere.includes( next.toArray().join(',') ) );
next.forEach( n => n.steps = pos.steps + 1 ); next.forEach( n => n.steps = pos.steps + 1 );
next.forEach( n => n.beenThere = [
...pos.beenThere, pos.toArray().join(',')
] );
potentials.push(...next); potentials.push(...next);
} }
return bestSoFar; return beenThere[final.x][final.y];
} }

View File

@ -5,17 +5,16 @@ import part1, { findStart, sample, puzzleInput } from "./part1.js";
import part2 from "./part2.js"; import part2 from "./part2.js";
describe("part 1", () => { describe("part 1", () => {
test( "findStart", () => { test("findStart", () => {
expect( findStart(sample).toArray() ).toEqual( expect(findStart(sample).toArray()).toEqual([0, 0]);
[0,0] });
) test.only("sample", () => {
expect(part1(sample)).toEqual(31);
}); });
test( "sample", async () => { test.only("solution",() => {
expect(part1(sample)).toEqual(31); const r = part1(puzzleInput);
}); expect(r).toBeLessThan(9999);
test.only("solution", () => { expectSolution(r).toEqual(361);
expectSolution(part1(puzzleInput)).toEqual("TODO");
}); });
}); });