Public
Edited
Dec 7
Paused
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function parse(input) {
const xs = input.split(/\s+/).map(Number);
return [xs.filter((_, i) => !(i % 2)), xs.filter((_, i) => i % 2)];
}
Insert cell
Insert cell
function part1(input) {
const [l1, l2] = parse(input).map((d) => d.sort());
return d3.sum(l1, (_, i) => Math.abs(l1[i] - l2[i]));
}
Insert cell
Insert cell
Insert cell
Insert cell
function part2(input) {
const [l1, l2] = parse(input);
const fs = l2.reduce((fs, x) => AOC.addToFreqTable(fs, x), new Map());
return d3.sum(l1, (d) => d * fs.get(d) || 0);
}
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