Public
Edited
Dec 6, 2021
1 star
Insert cell
Insert cell
Insert cell
Insert cell
function simulate(input, numDays) {
// Initialise with frequencies from input.
const totals = [0, 0, 0, 0, 0, 0, 0, 0, 0];
input.forEach((f) => {
totals[f]++;
});

for (let day = 0; day < numDays; day++) {
const newBirths = totals[0];
for (let i = 0; i < 8; i++) {
totals[i] = totals[i + 1];
}
totals[8] = newBirths;
totals[6] += newBirths;
}
return AOC.sum(totals);
}
Insert cell
function part1(input) {
return simulate(input, 80);
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function part2(input) {
return simulate(input, 256);
}
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