Public
Edited
Dec 10, 2022
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
headMoves = {
var x = 0,
y = 0;

return input
.flatMap((move) => {
switch (move.dir) {
case "L":
return _.range(0, move.n).map((_) => [x--, y]);
case "R":
return _.range(0, move.n).map((_) => [x++, y]);
case "U":
return _.range(0, move.n).map((_) => [x, y++]);
case "D":
return _.range(0, move.n).map((_) => [x, y--]);
}
})
.concat([[x, y]]);
}
Insert cell
tailMoves = {
var tailX = 0,
tailY = 0;

return headMoves.map(([headX, headY]) => {
if (
!(
headX - 1 <= tailX &&
tailX <= headX + 1 &&
headY - 1 <= tailY &&
tailY <= headY + 1
)
) {
tailX += headX > tailX ? 1 : headX < tailX ? -1 : 0;
tailY += headY > tailY ? 1 : headY < tailY ? -1 : 0;
}
return [tailX, tailY];
});
}
Insert cell
function tailMove(headX, headY) {
var tailX = 0,
tailY = 0;

if (
!(
headX - 1 <= tailX &&
tailX <= headX + 1 &&
headY - 1 <= tailY &&
tailY <= headY + 1
)
) {
tailX += headX > tailX ? 1 : headX < tailX ? -1 : 0;
tailY += headY > tailY ? 1 : headY < tailY ? -1 : 0;
}
return [tailX, tailY];
}
Insert cell
Insert cell
Insert cell
input01 = (await FileAttachment("input01.txt").text()).trim()
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