Public
Edited
Dec 9, 2023
1 fork
1 star
Insert cell
Insert cell
sample = `
0 3 6 9 12 15
1 3 6 10 15 21
10 13 16 21 30 45
`
Insert cell
parsed = input
.trim()
.split("\n")
.map((l) => l.split(" ").map(Number))
Insert cell
parsed[0]
Insert cell
function differences(list) {
const stack = [list];
while (!stack.at(-1).every((d) => d === 0))
stack.push(d3.pairs(stack.at(-1)).map(([a, b]) => b - a));
return stack;
}
Insert cell
function after(list) {
let n = 0;
for (let r of differences(list).reverse().slice(1)) {
n = r.at(-1) + n;
}
return n;
}
Insert cell
part1 = parsed.map(after).reduce((sum, n) => sum + n)
Insert cell
function before(list) {
let n = 0;
for (let r of differences(list).reverse().slice(1)) {
n = r[0] - n;
}
return n;
}
Insert cell
part2 = parsed.map(before).reduce((sum, n) => sum + n)
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