changes to day 12
This commit is contained in:
parent
f80142b6a7
commit
88dfa7677a
@ -10,7 +10,7 @@ const readInput = (...args) =>
|
|||||||
readFile(...args),
|
readFile(...args),
|
||||||
(lines) => lines.split("\n"),
|
(lines) => lines.split("\n"),
|
||||||
R.compact, // remove last line
|
R.compact, // remove last line
|
||||||
R.map((line) => line.split("").map( c => c ==='S' ? '`' : c === 'E' ? '{' : c ))
|
R.map( l => l.split('') ),
|
||||||
);
|
);
|
||||||
|
|
||||||
export const puzzleInput = readInput(import.meta.url, "input");
|
export const puzzleInput = readInput(import.meta.url, "input");
|
||||||
@ -19,7 +19,7 @@ export const sample = readInput(import.meta.url, "sample");
|
|||||||
export const findStart = (topoMap) => {
|
export const findStart = (topoMap) => {
|
||||||
let y;
|
let y;
|
||||||
let x = R.findIndex(topoMap, (row) => {
|
let x = R.findIndex(topoMap, (row) => {
|
||||||
let z = row.indexOf("`");
|
let z = row.indexOf("S");
|
||||||
if (z === -1) return false;
|
if (z === -1) return false;
|
||||||
y = z;
|
y = z;
|
||||||
return true;
|
return true;
|
||||||
@ -30,7 +30,7 @@ export const findStart = (topoMap) => {
|
|||||||
export const findEnd = (topoMap) => {
|
export const findEnd = (topoMap) => {
|
||||||
let y;
|
let y;
|
||||||
let x = R.findIndex(topoMap, (row) => {
|
let x = R.findIndex(topoMap, (row) => {
|
||||||
let z = row.indexOf("{");
|
let z = row.indexOf("E");
|
||||||
if (z === -1) return false;
|
if (z === -1) return false;
|
||||||
y = z;
|
y = z;
|
||||||
return true;
|
return true;
|
||||||
@ -62,6 +62,8 @@ function findShortestPath(topoMap) {
|
|||||||
initial.steps = 0;
|
initial.steps = 0;
|
||||||
const final = findEnd(topoMap);
|
const final = findEnd(topoMap);
|
||||||
|
|
||||||
|
topoMap = topoMap.map( line => line.map( c => c.replace( 'S', 'a' ).replace('E','z') ) );
|
||||||
|
|
||||||
const potentials = [ initial ];
|
const potentials = [ initial ];
|
||||||
|
|
||||||
const beenThere = topoMap.map(
|
const beenThere = topoMap.map(
|
||||||
|
@ -1,4 +1,90 @@
|
|||||||
import * as R from "remeda";
|
import * as R from "remeda";
|
||||||
|
import Victor from "@a-robu/victor";
|
||||||
|
|
||||||
|
import { readFile } from "../05/part1.js";
|
||||||
|
|
||||||
export default () => {};
|
const V = (x,y) => new Victor(x,y);
|
||||||
|
|
||||||
|
export const findAs = (topoMap) => {
|
||||||
|
const locations = [];
|
||||||
|
|
||||||
|
topoMap.forEach( (line,x) => line.forEach( (v,y) => {
|
||||||
|
if( v === 'a' ) locations.push( [x,y] )
|
||||||
|
} ) );
|
||||||
|
|
||||||
|
return locations;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const findEnd = (topoMap) => {
|
||||||
|
let y;
|
||||||
|
let x = R.findIndex(topoMap, (row) => {
|
||||||
|
let z = row.indexOf("E");
|
||||||
|
if (z === -1) return false;
|
||||||
|
y = z;
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
return V(x,y);
|
||||||
|
};
|
||||||
|
|
||||||
|
const directions = [ [0,1], [0,-1],[1,0],[-1,0] ].map(
|
||||||
|
d => V(...d)
|
||||||
|
);
|
||||||
|
|
||||||
|
const outOfMap = (maxX,maxY) => (loc) => {
|
||||||
|
if( Math.min( loc.x, loc.y ) < 0 ) return true;
|
||||||
|
if( loc.x >= maxX ) return true;
|
||||||
|
if( loc.y >= maxY ) return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const isReachable = (topoMap,pos) => {
|
||||||
|
const baseline = topoMap[pos.x][pos.y].charCodeAt(0);
|
||||||
|
|
||||||
|
return (next) => {
|
||||||
|
return baseline - topoMap[next.x][next.y].charCodeAt(0) <= 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function findShortestPath(topoMap) {
|
||||||
|
const final = findEnd(topoMap);
|
||||||
|
final.steps = 0;
|
||||||
|
|
||||||
|
topoMap = topoMap.map( line => line.map( c => c.replace( 'S', 'a' ).replace('E','z') ) );
|
||||||
|
|
||||||
|
const initial = findAs(topoMap);
|
||||||
|
|
||||||
|
const potentials = [ final ];
|
||||||
|
|
||||||
|
const beenThere = topoMap.map(
|
||||||
|
line => line.map( () => 9999 )
|
||||||
|
);
|
||||||
|
|
||||||
|
const oom = outOfMap(topoMap.length,topoMap[0].length );
|
||||||
|
|
||||||
|
let failsafe = 10; //topoMap.length*topoMap[0].length ;
|
||||||
|
while( potentials.length > 0 ) {
|
||||||
|
// if(! failsafe--) return;
|
||||||
|
const pos = potentials.shift(); // depth-first
|
||||||
|
|
||||||
|
if( beenThere[pos.x][pos.y] <= pos.steps ) continue;
|
||||||
|
beenThere[pos.x][pos.y] = pos.steps;
|
||||||
|
|
||||||
|
const next = directions.map(
|
||||||
|
d => d.clone().add(pos)
|
||||||
|
).filter( R.isNot(oom ))
|
||||||
|
.filter( isReachable(topoMap,pos) );
|
||||||
|
|
||||||
|
next.forEach( n => n.steps = pos.steps + 1 );
|
||||||
|
|
||||||
|
potentials.push(...next);
|
||||||
|
}
|
||||||
|
|
||||||
|
return R.pipe(
|
||||||
|
initial,
|
||||||
|
R.map(([x,y]) => beenThere[x][y]),
|
||||||
|
R.minBy( R.identity )
|
||||||
|
)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
export default findShortestPath;
|
||||||
|
@ -8,18 +8,21 @@ describe("part 1", () => {
|
|||||||
test("findStart", () => {
|
test("findStart", () => {
|
||||||
expect(findStart(sample).toArray()).toEqual([0, 0]);
|
expect(findStart(sample).toArray()).toEqual([0, 0]);
|
||||||
});
|
});
|
||||||
test.only("sample", () => {
|
test("sample", () => {
|
||||||
expect(part1(sample)).toEqual(31);
|
expect(part1(sample)).toEqual(31);
|
||||||
});
|
});
|
||||||
test.only("solution",() => {
|
test("solution",() => {
|
||||||
const r = part1(puzzleInput);
|
const r = part1(puzzleInput);
|
||||||
expect(r).toBeLessThan(9999);
|
expect(r).toBeLessThan(9999);
|
||||||
expectSolution(r).toEqual(361);
|
expectSolution(r).toEqual(361);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("part 2", () => {
|
describe.only("part 2", () => {
|
||||||
test.todo("solution", () => {
|
test("sample", () => {
|
||||||
expectSolution(part2(puzzleInput)).toEqual("TODO");
|
expect(part2(sample)).toEqual(29);
|
||||||
|
});
|
||||||
|
test("solution", () => {
|
||||||
|
expectSolution(part2(puzzleInput)).toEqual(354);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -6,9 +6,11 @@ import * as R from "remeda";
|
|||||||
import { readFile } from "../05/part1.js";
|
import { readFile } from "../05/part1.js";
|
||||||
|
|
||||||
const readInput = (...args) =>
|
const readInput = (...args) =>
|
||||||
readFile(...args)
|
R.pipe(
|
||||||
.split("\n")
|
readFile(...args),
|
||||||
.filter((x) => x);
|
(lines) => lines.split("\n"),
|
||||||
|
R.compact, // remove last line
|
||||||
|
);
|
||||||
|
|
||||||
export const puzzleInput = readInput(import.meta.url, "input");
|
export const puzzleInput = readInput(import.meta.url, "input");
|
||||||
export const sample = readInput(import.meta.url, "sample");
|
export const sample = readInput(import.meta.url, "sample");
|
||||||
|
3
_templates/package.json
Normal file
3
_templates/package.json
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"type": "commonjs"
|
||||||
|
}
|
@ -1,4 +1,5 @@
|
|||||||
{
|
{
|
||||||
|
"type": "module",
|
||||||
"name": "2022",
|
"name": "2022",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"description": "",
|
"description": "",
|
||||||
@ -16,6 +17,7 @@
|
|||||||
"memoizerific": "^1.11.3",
|
"memoizerific": "^1.11.3",
|
||||||
"prettier": "^2.8.0",
|
"prettier": "^2.8.0",
|
||||||
"remeda": "^1.3.0",
|
"remeda": "^1.3.0",
|
||||||
"vitest": "^0.25.3"
|
"vite": "^4.0.1",
|
||||||
|
"vitest": "0.25.7"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user