Public
Edited
Aug 26, 2023
1 fork
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
directions = ({
U: [0, -1],
D: [0, 1],
L: [-1, 0],
R: [1, 0]
})
Insert cell
Insert cell
async function validMoves(x, y, path, passcode) {
const hash = await MD5Lib.md5(passcode + path);
// Movement limited to a 4x4 grid:
const directions = {
U: y > 0,
D: y < 3,
L: x > 0,
R: x < 3
};
return ["U", "D", "L", "R"].filter(
(dir, i) => directions[dir] && hash[i] > "a" && hash[i] <= "f"
);
}
Insert cell
Insert cell
async function shortestPath(passcode) {
const queue = [{ x: 0, y: 0, path: "" }];
while (queue.length > 0) {
const { x, y, path } = queue.shift();
if (x === 3 && y === 3) {
return path; // We've reached the vault.
}
for (const move of await validMoves(x, y, path, passcode)) {
const [dx, dy] = directions[move];
queue.push({ x: x + dx, y: y + dy, path: path + move });
}
}
}
Insert cell
part1 = shortestPath(puzzleInput)
Insert cell
Insert cell
Insert cell
Insert cell
async function longestPath(x, y, path, passcode) {
if (x === 3 && y === 3) {
return path.length; // We've reached the vault.
}
const validDirs = await validMoves(x, y, path, passcode);
const lengths = await Promise.all(
validDirs.map(async (dir) => {
const [dx, dy] = directions[dir];
return longestPath(x + dx, y + dy, path + dir, passcode);
})
);
return Math.max(...lengths);
}
Insert cell
part2 = await longestPath(0, 0, "", puzzleInput)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more