Public
Edited
Dec 1, 2023
Insert cell
Insert cell
Insert cell
function move([x, y], instr) {
if (instr === "<") return [x - 1, y];
if (instr === ">") return [x + 1, y];
if (instr === "^") return [x, y + 1];
if (instr === "v") return [x, y - 1];
}
Insert cell
Insert cell
function part1(input) {
const visited = new Set(["0,0"]);
let location = [0, 0];

for (const instr of input) {
location = move(location, instr);
visited.add(location.join(","));
}
return visited.size;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function part2(input) {
const visited = new Set(["0,0"]);
let santa = [0, 0];
let roboSanta = [0, 0];
let useRobo = false;

for (const instr of input) {
if (useRobo) {
roboSanta = move(roboSanta, instr);
visited.add(roboSanta.join(","));
} else {
santa = move(santa, instr);
visited.add(santa.join(","));
}
useRobo = !useRobo;
}
return visited.size;
}
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