Published
Edited
Dec 7, 2021
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// Copy of https://observablehq.com/@visnup/day-6-lanternfish#step
stepObject = (fishes) => {
const next = {};
for (const [n, count] of Object.entries(fishes)) { // run through all fish timers
if (n > 0) { //
next[n - 1] = (next[n - 1] || 0) + count;
} else { // n === 0 -> reset to 6 implies incrementing timer 6; idem timer 8
next[6] = (next[6] || 0) + count;
next[8] = (next[8] || 0) + count;
}
}
return next;
}
Insert cell
runnerObject = (data, n) => {
while (n--) data = stepObject(data);
return _.sum(Object.values(data))
}
Insert cell
Insert cell
fishesMap = countBy(data)
Insert cell
countBy = (fishes) => {
const a = new Map();
for (const k of fishes) a.set(k, (a.get(k) || 0) + 1);
return a;
}
Insert cell
stepMap = (fishes) => {
const next = new Map();
for (const [timer, count] of fishes) {
if (timer > 0) {
next.set(timer - 1, (next.get(timer - 1) || 0) + count);
} else {
next.set(6, (next.get(6) || 0) + count);
next.set(8, (next.get(8) || 0) + count);
}
}
return next;
}
Insert cell
runnerMap = (data, n) => {
while (n--) data = stepMap(data);
let sum = 0;
for (let [_, fishes] of data) sum += fishes;
return sum;
}
Insert cell
Insert cell
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