Try with branch caching again
This commit is contained in:
@@ -106,8 +106,6 @@ for (let y = 1; y < height - 1; y++) {
|
|||||||
|
|
||||||
let fastest: number|null = null;
|
let fastest: number|null = null;
|
||||||
|
|
||||||
const scoreCache: { [key: string]: [number, Coord, Dir] } = {};
|
|
||||||
|
|
||||||
const branchCache: { [key: string]: number } = {};
|
const branchCache: { [key: string]: number } = {};
|
||||||
|
|
||||||
const scores: Array<[number, Array<string>]> = [];
|
const scores: Array<[number, Array<string>]> = [];
|
||||||
@@ -125,9 +123,6 @@ function getScoresAfterPosition(pos: Coord, dir: Dir, currentScore = 0, path: Ar
|
|||||||
} else {
|
} else {
|
||||||
branchCache[cacheKey] = currentScore;
|
branchCache[cacheKey] = currentScore;
|
||||||
}
|
}
|
||||||
if (lastBranch) {
|
|
||||||
scoreCache[`${lastBranch[1].x},${lastBranch[1].y}`] = [currentScore - lastBranch[0], pos, dir];
|
|
||||||
}
|
|
||||||
lastBranch = [currentScore, pos];
|
lastBranch = [currentScore, pos];
|
||||||
}
|
}
|
||||||
[dir, nextDir(dir), prevDir(dir)].forEach((newDir, i) => {
|
[dir, nextDir(dir), prevDir(dir)].forEach((newDir, i) => {
|
||||||
|
|||||||
105
2024/18/index.ts
105
2024/18/index.ts
@@ -58,9 +58,27 @@ function prevDir(dir: Dir): Dir {
|
|||||||
return directions[i];
|
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());
|
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(mapClone.map((row: Array<string>) => row.join('')).join("\n"));
|
||||||
|
console.log("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
function nextCoord(pos: Coord, direction: Dir): Coord {
|
function nextCoord(pos: Coord, direction: Dir): Coord {
|
||||||
@@ -93,7 +111,6 @@ function fillDeadEnd(pos: Coord): void {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
printMap();
|
printMap();
|
||||||
console.log("\n");
|
|
||||||
|
|
||||||
for (let y = 0; y < height; y++) {
|
for (let y = 0; y < height; y++) {
|
||||||
for (let x = 0; x < width; x++) {
|
for (let x = 0; x < width; x++) {
|
||||||
@@ -110,39 +127,51 @@ for (let y = 0; y < height; y++) {
|
|||||||
|
|
||||||
printMap();
|
printMap();
|
||||||
|
|
||||||
//let fastest: number|null = null;
|
let fastest: number|null = null;
|
||||||
//
|
|
||||||
//const scoreCache: { [key: string]: [number, Coord, Dir] } = {};
|
const scoreCache: { [key: string]: [number, Coord, Dir] } = {};
|
||||||
//
|
|
||||||
//const branchCache: { [key: string]: number } = {};
|
const branchCache: { [key: string]: number } = {};
|
||||||
//
|
|
||||||
//const scores: Array<number> = [];
|
const scores: Array<Array<Coord>> = [];
|
||||||
//
|
|
||||||
//function getScoresAfterPosition(pos: Coord, dir: Dir, path: Array<string> = []): void {
|
function getScoresAfterPosition(pos: Coord, dir: Dir, path: Array<Coord> = [], lastBranch: [number, Coord]|null = null): void {
|
||||||
// if (fastest !== null && path.length >= fastest) {
|
if (fastest !== null && path.length >= fastest) {
|
||||||
// return;
|
return;
|
||||||
// }
|
}
|
||||||
// [dir, nextDir(dir), prevDir(dir)].forEach((newDir, i) => {
|
const branch = isBranch(pos, dir);
|
||||||
// const next = nextCoord(pos, newDir);
|
if (branch) {
|
||||||
// const nextStr = `${next.x},${next.y}`;
|
const cacheKey = `${pos.x},${pos.y},${dir}`;
|
||||||
// if (!path.includes(nextStr)) {
|
const cache = branchCache[cacheKey];
|
||||||
// if (next.x === width - 1 && next.y === height - 1) {
|
if (cache && cache < path.length) {
|
||||||
// if (fastest === null || path.length < fastest) {
|
return;
|
||||||
// fastest = path.length;
|
} else {
|
||||||
// }
|
branchCache[cacheKey] = path.length;
|
||||||
// scores.push(path.length);
|
}
|
||||||
// return;
|
lastBranch = [path.length, pos];
|
||||||
// } else if (next.x >= 0 && next. x < width && next.y >= 0 && next.y < height && get(next) === '.') {
|
}
|
||||||
// getScoresAfterPosition(next, newDir, [...path, nextStr]);
|
[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) {
|
||||||
// return;
|
fastest = path.length;
|
||||||
//}
|
}
|
||||||
//
|
scores.push(path);
|
||||||
//getScoresAfterPosition({ x: 0, y: 0 }, Dir.Up, ['0,0']);
|
return;
|
||||||
//
|
} else if (next.x >= 0 && next. x < width && next.y >= 0 && next.y < height && get(next) === '.') {
|
||||||
//console.log(scores);
|
getScoresAfterPosition(next, newDir, [...path, next], lastBranch);
|
||||||
//const fastestScore = Math.min(...scores);
|
}
|
||||||
//
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
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);
|
||||||
|
|||||||
Reference in New Issue
Block a user