Compare commits

..

4 Commits

Author SHA1 Message Date
64d12de0f4 Day 18 2024-12-19 00:06:50 +00:00
f4e5be11bf Conflict 2024-12-18 23:24:08 +00:00
7b18064b75 Try A star algorithm 2024-12-18 23:22:08 +00:00
2dc097383c Try with branch caching again 2024-12-18 23:21:58 +00:00
3 changed files with 208 additions and 48 deletions

View File

@@ -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) => {

View File

@@ -14,7 +14,7 @@ enum Dir {
Left, Left,
}; };
const map: Array<Array<string>> = []; const map: Array<Array<string|number>> = [];
const width = 71; const width = 71;
const height = 71; const height = 71;
@@ -33,14 +33,14 @@ for (let i = 0; i < 1024; i++) {
map[y][x] = '#' map[y][x] = '#'
} }
function get(pos: Coord): string { function get(pos: Coord): string|number {
if (map[pos.y] && map[pos.y][pos.x]) { if (map[pos.y] && map[pos.y][pos.x]) {
return map[pos.y][pos.x]; return map[pos.y][pos.x];
} }
return '#'; return '#';
} }
function set(pos: Coord, val: string): void { function set(pos: Coord, val: string|number): void {
map[pos.y][pos.x] = val; map[pos.y][pos.x] = val;
} }
@@ -58,9 +58,13 @@ function prevDir(dir: Dir): Dir {
return directions[i]; return directions[i];
} }
function printMap() { function printMap(path: Array<Coord> = []) {
const mapClone = map.slice().map((row: Array<string>) => row.slice()); const mapClone = map.slice().map((row: Array<string|number>) => row.slice());
console.log(mapClone.map((row: Array<string>) => row.join('')).join("\n")); path.forEach(({ x, y }) => {
mapClone[y][x] = 'O';
});
console.log(mapClone.map((row: Array<string|number>) => row.map((c) => typeof c === 'number' ? '.' : c).join('')).join("\n"));
console.log("\n");
} }
function nextCoord(pos: Coord, direction: Dir): Coord { function nextCoord(pos: Coord, direction: Dir): Coord {
@@ -93,7 +97,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 +113,38 @@ 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 scores: Array<Array<Coord>> = [];
//
//const branchCache: { [key: string]: number } = {}; function getScoresAfterPosition(pos: Coord, dir: Dir, path: Array<Coord> = [], lastBranch: [number, Coord]|null = null): void {
// if (fastest !== null && path.length >= fastest) {
//const scores: Array<number> = []; return;
// }
//function getScoresAfterPosition(pos: Coord, dir: Dir, path: Array<string> = []): void { [dir, nextDir(dir), prevDir(dir)].forEach((newDir, i) => {
// if (fastest !== null && path.length >= fastest) { const next = nextCoord(pos, newDir);
// return; const nextCell = get(next);
// } if (nextCell !== '#' && (typeof nextCell !== 'number' || nextCell > path.length) && !path.find(({ x, y }) => next.x === x && next.y === y)) {
// [dir, nextDir(dir), prevDir(dir)].forEach((newDir, i) => { if (next.x === width - 1 && next.y === height - 1) {
// const next = nextCoord(pos, newDir); if (fastest === null || path.length < fastest) {
// const nextStr = `${next.x},${next.y}`; fastest = path.length;
// if (!path.includes(nextStr)) { }
// if (next.x === width - 1 && next.y === height - 1) { scores.push(path);
// if (fastest === null || path.length < fastest) { return;
// fastest = path.length; } else if (next.x >= 0 && next.x < width && next.y >= 0 && next.y < height) {
// } set(next, path.length);
// scores.push(path.length); getScoresAfterPosition(next, newDir, [...path, next], lastBranch);
// return; }
// } else if (next.x >= 0 && next. x < width && next.y >= 0 && next.y < height && get(next) === '.') { }
// getScoresAfterPosition(next, newDir, [...path, nextStr]); });
// }
// } return;
// }); }
//
// return; getScoresAfterPosition({ x: 0, y: 0 }, Dir.Up, [{ x: 0, y: 0 }]);
//}
// console.log(scores);
//getScoresAfterPosition({ x: 0, y: 0 }, Dir.Up, ['0,0']); const fastestScore = Math.min(...scores.map((p) => p.length));
// const fastestPath = scores.find((p) => p.length === fastestScore);
//console.log(scores); printMap(fastestPath);
//const fastestScore = Math.min(...scores); console.log(fastestScore);
//

163
2024/18/index2.ts Normal file
View File

@@ -0,0 +1,163 @@
const fs = require('fs');
const input = fs.readFileSync(__dirname + '/input.txt', 'utf8').split("\n");
interface Coord {
x: number,
y: number,
};
enum Dir {
Up,
Right,
Down,
Left,
};
const map: Array<Array<string|number>> = [];
const width = 71;
const height = 71;
for (let y = 0; y < height; y++) {
if (!map[y]) {
map[y] = [];
}
for (let x = 0; x < width; x++) {
map[y][x] = '.';
}
}
for (let i = 0; i < 1024; i++) {
const [x, y] = input[i].split(',').map((st: string) => parseInt(st));
map[y][x] = '#'
}
function get(pos: Coord): string|number {
if (map[pos.y] && map[pos.y][pos.x]) {
return map[pos.y][pos.x];
}
return '#';
}
function set(pos: Coord, val: string|number): void {
map[pos.y][pos.x] = val;
}
function nextDir(dir: Dir): Dir {
const directions = [Dir.Up, Dir.Right, Dir.Down, Dir.Left];
const i = (directions.indexOf(dir) + 1) % directions.length;
return directions[i];
}
function prevDir(dir: Dir): Dir {
const directions = [Dir.Up, Dir.Right, Dir.Down, Dir.Left];
const i = directions.indexOf(dir) - 1;
if (i < 0) {
return directions[directions.length - 1];
}
return directions[i];
}
function printMap(path: Array<Coord> = []) {
const mapClone = map.slice().map((row: Array<string|number>) => row.slice());
path.forEach(({ x, y }) => {
mapClone[y][x] = 'O';
});
console.log(mapClone.map((row: Array<string|number>) => row.map((c) => typeof c === 'number' ? '.' : c).join('')).join("\n"));
console.log("\n");
}
function nextCoord(pos: Coord, direction: Dir): Coord {
return direction === Dir.Up ? { x: pos.x, y: pos.y - 1 }
: (direction === Dir.Right ? { x: pos.x + 1, y: pos.y }
: (direction === Dir.Down ? { x: pos.x, y: pos.y + 1 } : { x: pos.x - 1, y: pos.y })
);
}
function isDeadEnd(pos: Coord): boolean {
if ((pos.x === 0 && pos.y === 0) || (pos.x === width - 1 && pos.y === width - 1)) {
return false;
}
const adjs = [Dir.Up, Dir.Right, Dir.Down, Dir.Left].map((dir) => get(nextCoord(pos, dir))).join('');
return adjs === '###.'
|| adjs === '##.#'
|| adjs === '#.##'
|| adjs === '.###';
}
function fillDeadEnd(pos: Coord): void {
set(pos, '#')
for (let dir of [Dir.Up, Dir.Right, Dir.Down, Dir.Left]) {
const next = nextCoord(pos, dir);
if (get(next) === '.') {
if (isDeadEnd(next)) {
fillDeadEnd(next);
}
}
};
}
printMap();
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
const pos = { x, y };
const char = get(pos);
if (char !== '.') {
continue;
}
if (isDeadEnd(pos)) {
fillDeadEnd(pos);
}
}
}
printMap();
for (let i = 1025; i < input.length; i++) {
const byte = input[i].split(',').map((c: string) => parseInt(c));
set({ x: byte[0], y: byte[1] }, '#');
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
const pos = { x, y };
let char = get(pos);
if (typeof char === 'number') {
set(pos, '.');
}
}
}
let finished = false;
function getScoresAfterPosition(pos: Coord, dir: Dir, path: Array<Coord> = []): void {
if (finished) {
return;
}
[dir, nextDir(dir), prevDir(dir)].forEach((newDir, i) => {
if (finished) {
return;
}
const next = nextCoord(pos, newDir);
if (next.x === width - 1 && next.y === height - 1) {
finished = true;
return;
}
const nextCell = get(next);
if (nextCell !== '#' && (typeof nextCell !== 'number' || nextCell > path.length) && !path.find(({ x, y }) => next.x === x && next.y === y)) {
set(next, path.length);
getScoresAfterPosition(next, newDir, [...path, next]);
}
});
return;
}
getScoresAfterPosition({ x: 0, y: 0 }, Dir.Right, [{ x: 0, y: 0 }]);
console.log(i, byte);
printMap();
if (!finished) {
console.log('blocked');
break;
}
}