Public
Edited
Sep 4, 2022
1 fork
1 star
Insert cell
Insert cell
Insert cell
function parse(input) {
return input
.split("\n")
.map((s) => s.split(" "))
.map(([instr, val]) =>
instr === "forward"
? [Number(val), 0]
: instr === "down"
? [0, Number(val)]
: [0, -Number(val)]
);
}
Insert cell
Insert cell
Insert cell
function part1(input) {
const move = ([x, y], [x1, y1]) => [x + x1, y + y1];
return AOC.product(parse(input).reduce(move, [0, 0]));
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function part2(input) {
const move = ([x, y, aim], [x1, y1]) => [x + x1, y + aim * x1, aim + y1];
return AOC.product(parse(input).reduce(move, [0, 0, 0]).slice(0, 2));
}
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