Public
Edited
Dec 1, 2023
Insert cell
Insert cell
Insert cell
function wallAt(x, y) {
return (
(x * x + y * y + 2 * x * y + 3 * x + y + puzzleInput)
.toString(2)
.split("")
.filter((b) => b === "1").length % 2
);
}
Insert cell
Insert cell
dirs = [
[0, -1], // N
[0, 1], // S
[-1, 0], // E
[1, 0] // W
]
Insert cell
Insert cell
function shortestPath(target) {
const queue = [[1, 1, 0]]; // Starting point (1,1) and initial steps (0)
const visited = new Set(["1,1"]);
while (queue.length) {
const [x, y, steps] = queue.shift();
if (x === target[0] && y === target[1]) {
return steps;
}
for (const [dx, dy] of dirs) {
const [x2, y2] = [x + dx, y + dy];
if (
x2 >= 0 &&
y2 >= 0 &&
!wallAt(x2, y2) &&
!visited.has(`${x2},${y2}`)
) {
visited.add(`${x2},${y2}`);
queue.push([x2, y2, steps + 1]);
}
}
}
}
Insert cell
function part1(input) {
return shortestPath([31, 39]);
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function reachableLocationsInSteps(maxSteps) {
const queue = [[1, 1, 0]]; // Starting point (1,1) and initial steps (0)
const visited = new Set(["1,1"]);
while (queue.length) {
const [x, y, steps] = queue.shift();
if (steps >= maxSteps) {
return visited.size;
}
for (const [dx, dy] of dirs) {
const [x2, y2] = [x + dx, y + dy];
if (
x2 >= 0 &&
y2 >= 0 &&
!wallAt(x2, y2) &&
!visited.has(`${x2},${y2}`)
) {
visited.add(`${x2},${y2}`);
queue.push([x2, y2, steps + 1]);
}
}
}
return visited.size;
}
Insert cell
function part2(input) {
return reachableLocationsInSteps(50);
}
Insert cell
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