Skip to content

Commit

Permalink
Add day 20 of year 2024 🎄
Browse files Browse the repository at this point in the history
  • Loading branch information
letelete committed Dec 21, 2024
1 parent a344c0c commit 6d82d29
Showing 1 changed file with 42 additions and 32 deletions.
74 changes: 42 additions & 32 deletions 2024/days/day-20/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,17 @@ function parse(source) {

function findTile(grid, char) {
const row = grid.findIndex((row) => row.includes(char));
return [row, grid[row].indexOf(char)];
const col = grid[row].findIndex((col) => col === char);
return [row, col];
}

const symbols = { start: 'S', end: 'E', wall: '#', track: '.' };
const symbols = {
start: 'S',
end: 'E',
wall: '#',
track: '.',
};

const dirs = {
orthogonal: [
[0, -1],
Expand All @@ -24,35 +31,32 @@ function inRange(data, row, col) {
return row >= 0 && row < data.length && col >= 0 && col < data[0].length;
}

const hash = (row, col) => `${row},${col}`;

function bfs(grid, start, end, visited, trackPath, memo = new Map()) {
const startKey = `${hash(...start)}->${hash(...end)}`;
if (memo.has(startKey)) return memo.get(startKey);
function hash(row, col) {
return `${row},${col}`;
}

const q = [[0, start]],
source = new Map();
function bfs(grid, start, end, visited, trackPath) {
const q = [[0, start]];
const source = new Map();

while (q.length) {
const [steps, [row, col]] = q.shift();
const key = hash(row, col);
if (visited.has(key)) continue;
visited.add(key);
if (visited.has(hash(row, col))) {
continue;
}
visited.add(hash(row, col));

if (row === end[0] && col === end[1]) {
if (!trackPath) {
memo.set(startKey, steps);
return steps;
}
const path = [[row, col]];
let head = source.get(key);
let head = source.get(hash(row, col));
while (head) {
path.push(head.split(',').map(Number));
head = source.get(head);
}
const result = [steps, path.reverse()];
memo.set(startKey, result);
return result;
return [steps, [...path].reverse()];
}

dirs.orthogonal.forEach(([drow, dcol]) => {
Expand All @@ -63,44 +67,50 @@ function bfs(grid, start, end, visited, trackPath, memo = new Map()) {
!visited.has(hash(nextRow, nextCol))
) {
q.push([steps + 1, [nextRow, nextCol]]);
source.set(hash(nextRow, nextCol), key);
source.set(hash(nextRow, nextCol), hash(row, col));
}
});
}
memo.set(startKey, -1);

return -1;
}

function part1(data) {
const end = findTile(data, symbols.end),
start = findTile(data, symbols.start);
const memo = new Map();
const [minSteps, path] = bfs(data, start, end, new Set(), true, memo);
const end = findTile(data, symbols.end);

const [minSteps, path] = bfs(
data,
findTile(data, symbols.start),
end,
new Set(),
true
);

const visited = new Set(),
saves = new Map();
const visited = new Set();
const saves = new Map();
console.log({ path });
path.forEach(([row, col], steps) => {
visited.add(hash(row, col));
dirs.orthogonal.forEach(([drow, dcol]) => {
const [nextRow, nextCol] = [row + drow, col + dcol];
const nextKey = hash(nextRow, nextCol);
const nextHash = hash(nextRow, nextCol);
if (
inRange(data, nextRow, nextCol) &&
data[nextRow][nextCol] === symbols.wall &&
!visited.has(nextKey)
!visited.has(nextHash)
) {
const bfsres = bfs(
data,
[nextRow, nextCol],
end,
new Set(visited),
false,
memo
new Set(...visited),
false
);
if (bfsres !== -1) {
const candidate = steps + bfsres + 1;
if (candidate < minSteps)
if (candidate < minSteps) {
saves.set(candidate, (saves.get(candidate) ?? 0) + 1);
}
}
}
});
Expand All @@ -114,7 +124,7 @@ function part1(data) {
];
}

function part2() {
function part2(data) {
return null;
}

Expand Down

0 comments on commit 6d82d29

Please sign in to comment.