Public
Edited
Dec 13, 2022
1 fork
1 star
Insert cell
Insert cell
part2 = (sorted.indexOf(dividers[0]) + 1) * (sorted.indexOf(dividers[1]) + 1)
Insert cell
sorted = pairs.flat().concat(dividers).sort(compare)
Insert cell
dividers = [[[2]], [[6]]]
Insert cell
part1 = comparisons
.filter((r) => r[1] === -1)
.reduce((sum, d) => sum + d[0], 0)
Insert cell
comparisons = pairs.map((p, i) => [i + 1, compare(...p)])
Insert cell
function compare(l, r) {
if (typeof l === "number" && typeof r === "number") {
if (l < r) return -1;
if (l > r) return 1;
} else if (typeof l === "object" && typeof r === "object") {
for (const [left, right] of d3.zip(l, r)) {
const c = compare(left, right);
if (c) return c;
}
if (l.length < r.length) return -1;
if (l.length > r.length) return 1;
} else if (typeof l === "number") {
return compare([l], r);
} else {
return compare(l, [r]);
}
return 0;
}
Insert cell
pairs = data
.trim()
.split("\n\n")
.map((p) => p.split("\n").map(JSON.parse))
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