Public
Edited
Dec 9, 2023
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

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more