Public
Edited
Dec 4, 2023
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function parse(input) {
return input
.split("\n")
.map((line) =>
line
.split(":")[1]
.split("|")
.map((nums) => nums.match(/\d+/g))
)
.map(([win, numbers]) => {
const set = new Set(win);
return numbers.filter((n) => set.has(n)).length;
});
}
Insert cell
Insert cell
Insert cell
function part1(input) {
return _.sum(parse(input).map((n) => (n === 0 ? 0 : 2 ** (n - 1))));
}
Insert cell
Insert cell
Insert cell
Insert cell
function part2(input) {
const matches = parse(input);
const copies = Array(matches.length).fill(1);

for (let i = 0; i < copies.length; i++) {
for (let j = 1; j <= matches[i] && i + j < copies.length; j++) {
copies[i + j] += copies[i];
}
}
return _.sum(copies);
}
Insert cell
Insert cell
Insert cell
input = textarea || select.value
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