Public
Edited
Dec 2
Paused
1 fork
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function parse(input) {
return input.split("\n").map((l) => l.split(""));
}
Insert cell
Insert cell
function blank(grid) {
const [nRows, nCols] = [grid.length, grid[0].length];
return AOC.gMapWithLocation(
(r, c, v) =>
r === 0 || r === nRows - 1 || c === 0 || c === nCols - 1 ? v : ".",
grid
);
}
Insert cell
function gridAt(grid, t) {
const [nRows, nCols] = [grid.length, grid[0].length];
const grid2 = blank(grid);
for (let r = 1; r < nRows - 1; r++) {
for (let c = 1; c < nCols - 1; c++) {
if (grid[r][c] === ">") {
grid2[r][((c - 1 + t) % (nCols - 2)) + 1] = ">";
} else if (grid[r][c] === "<") {
grid2[r][((c - 1 + t * (nCols - 2) - t) % (nCols - 2)) + 1] = "<";
} else if (grid[r][c] === "v") {
grid2[((r - 1 + t) % (nRows - 2)) + 1][c] = "v";
} else if (grid[r][c] === "^") {
grid2[((r - 1 + t * (nRows - 2) - t) % (nRows - 2)) + 1][c] = "^";
}
}
}
return grid2;
}
Insert cell
Insert cell
function move(initGrid, [rowStart, colStart], [rowEnd, colEnd], t) {
const [nRows, nCols] = [initGrid.length, initGrid[0].length];
const visited = blank(initGrid);
visited[rowStart][colStart] = t;
while (visited[rowEnd][colEnd] === ".") {
const grid = gridAt(initGrid, t + 1);
const spaces = [];

// Where can we get to in one step from all the cells that could be visited at this time?
for (let row = 0; row < nRows; row++) {
for (let col = 0; col < nCols; col++) {
if (visited[row][col] === t) {
if (col < nCols - 1 && grid[row][col + 1] === ".") {
spaces.push([row, col + 1]); // Can move right
}
if (row < nRows - 1 && grid[row + 1][col] === ".") {
spaces.push([row + 1, col]); // Can move down
}
if (grid[row][col] === ".") {
spaces.push([row, col]); // Can wait
}
if (row > 0 && grid[row - 1][col] === ".") {
spaces.push([row - 1, col]); // Can move up
}
if (col > 0 && grid[row][col - 1] === ".") {
spaces.push([row, col - 1]); // Can move left
}
}
}
}
spaces.forEach(([row, col]) => (visited[row][col] = t + 1));
t++;
}
return t;
}
Insert cell
function part1(input) {
const initGrid = parse(input);
const [nRows, nCols] = [initGrid.length, initGrid[0].length];
return move(initGrid, [0, 1], [nRows - 1, nCols - 2], 0);
}
Insert cell
Insert cell
Insert cell
Insert cell
function part2(input) {
const initGrid = parse(input);
const [nRows, nCols] = [initGrid.length, initGrid[0].length];
const topLeft = [0, 1];
const bottomRight = [nRows - 1, nCols - 2];

const trip1 = move(initGrid, topLeft, bottomRight, 0);
const trip2 = move(initGrid, bottomRight, topLeft, trip1);
return move(initGrid, topLeft, bottomRight, trip2);
}
Insert cell
Insert cell
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