Public
Edited
Dec 4, 2023
Insert cell
Insert cell
cards = input
.trim()
.split("\n")
.map((line, i) => {
const [, winning, have] = line
.split(/[:|]/)
.map((d) => d && new Set(d.trim().split(/\s+/).map(Number)));
return { i, winning, have, won: d3.intersection(winning, have).size };
})
Insert cell
Insert cell
part1 = d3.sum(cards, ({ won }) => Math.sign(won) << (won - 1))
Insert cell
Insert cell
part2 = {
const q = [...cards];
let seen = 0;
while (q.length) {
const {i, won} = q.shift();
for (let j = 1; j <= won; j++) q.push(cards[i+j]);
seen++;
}
return seen;
}
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