Try with branch caching again

This commit is contained in:
2024-12-18 23:21:58 +00:00
parent f79cf1a347
commit 2dc097383c
2 changed files with 67 additions and 43 deletions

View File

@@ -106,8 +106,6 @@ for (let y = 1; y < height - 1; y++) {
let fastest: number|null = null;
const scoreCache: { [key: string]: [number, Coord, Dir] } = {};
const branchCache: { [key: string]: number } = {};
const scores: Array<[number, Array<string>]> = [];
@@ -125,9 +123,6 @@ function getScoresAfterPosition(pos: Coord, dir: Dir, currentScore = 0, path: Ar
} else {
branchCache[cacheKey] = currentScore;
}
if (lastBranch) {
scoreCache[`${lastBranch[1].x},${lastBranch[1].y}`] = [currentScore - lastBranch[0], pos, dir];
}
lastBranch = [currentScore, pos];
}
[dir, nextDir(dir), prevDir(dir)].forEach((newDir, i) => {

View File

@@ -58,9 +58,27 @@ function prevDir(dir: Dir): Dir {
return directions[i];
}
function printMap() {
function isBranch(pos: Coord, dir: Dir): boolean {
const leftDir = prevDir(dir);
const rightDir = nextDir(dir);
const next = nextCoord(pos, dir);
const left = nextCoord(pos, leftDir);
const right = nextCoord(pos, rightDir);
const nextChar = get(next);
const leftChar = get(left);
const rightChar = get(right);
return nextChar === '.' && leftChar === '.'
|| nextChar === '.' && rightChar === '.'
|| leftChar === '.' && rightChar === '.';
}
function printMap(path: Array<Coord> = []) {
const mapClone = map.slice().map((row: Array<string>) => row.slice());
path.forEach(({ x, y }) => {
mapClone[y][x] = 'O';
});
console.log(mapClone.map((row: Array<string>) => row.join('')).join("\n"));
console.log("\n");
}
function nextCoord(pos: Coord, direction: Dir): Coord {
@@ -93,7 +111,6 @@ function fillDeadEnd(pos: Coord): void {
};
}
printMap();
console.log("\n");
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
@@ -110,39 +127,51 @@ for (let y = 0; y < height; y++) {
printMap();
//let fastest: number|null = null;
//
//const scoreCache: { [key: string]: [number, Coord, Dir] } = {};
//
//const branchCache: { [key: string]: number } = {};
//
//const scores: Array<number> = [];
//
//function getScoresAfterPosition(pos: Coord, dir: Dir, path: Array<string> = []): void {
// if (fastest !== null && path.length >= fastest) {
// return;
// }
// [dir, nextDir(dir), prevDir(dir)].forEach((newDir, i) => {
// const next = nextCoord(pos, newDir);
// const nextStr = `${next.x},${next.y}`;
// if (!path.includes(nextStr)) {
// if (next.x === width - 1 && next.y === height - 1) {
// if (fastest === null || path.length < fastest) {
// fastest = path.length;
// }
// scores.push(path.length);
// return;
// } else if (next.x >= 0 && next. x < width && next.y >= 0 && next.y < height && get(next) === '.') {
// getScoresAfterPosition(next, newDir, [...path, nextStr]);
// }
// }
// });
//
// return;
//}
//
//getScoresAfterPosition({ x: 0, y: 0 }, Dir.Up, ['0,0']);
//
//console.log(scores);
//const fastestScore = Math.min(...scores);
//
let fastest: number|null = null;
const scoreCache: { [key: string]: [number, Coord, Dir] } = {};
const branchCache: { [key: string]: number } = {};
const scores: Array<Array<Coord>> = [];
function getScoresAfterPosition(pos: Coord, dir: Dir, path: Array<Coord> = [], lastBranch: [number, Coord]|null = null): void {
if (fastest !== null && path.length >= fastest) {
return;
}
const branch = isBranch(pos, dir);
if (branch) {
const cacheKey = `${pos.x},${pos.y},${dir}`;
const cache = branchCache[cacheKey];
if (cache && cache < path.length) {
return;
} else {
branchCache[cacheKey] = path.length;
}
lastBranch = [path.length, pos];
}
[dir, nextDir(dir), prevDir(dir)].forEach((newDir, i) => {
const next = nextCoord(pos, newDir);
if (!path.find(({ x, y }) => next.x === x && next.y === y)) {
if (next.x === width - 1 && next.y === height - 1) {
if (fastest === null || path.length < fastest) {
fastest = path.length;
}
scores.push(path);
return;
} else if (next.x >= 0 && next. x < width && next.y >= 0 && next.y < height && get(next) === '.') {
getScoresAfterPosition(next, newDir, [...path, next], lastBranch);
}
}
});
return;
}
getScoresAfterPosition({ x: 0, y: 0 }, Dir.Up, [{ x: 0, y: 0 }]);
console.log(scores);
const fastestScore = Math.min(...scores.map((p) => p.length));
const fastestPath = scores.find((p) => p.length === fastestScore);
printMap(fastestPath);
console.log(fastestScore);