Published
Edited
Dec 14, 2021
2 stars
Insert cell
Insert cell
Insert cell
part2 = {
for (const [c] of counts(40)) {
const [min, max] = d3.extent(c.values());
yield max - min;
}
}
Insert cell
function* counts(n) {
let s = [
countBy(template),
countBy(d3.pairs(template), (p) => p.join(""))
];
yield s;
while (n--) yield (s = count(...s));
}
Insert cell
count = (letters, pairs) => {
letters = new Map(letters);
const next = new Map();
for (const [p, c] of pairs.entries()) {
const m = rules.get(p);
letters.set(m, (letters.get(m) || 0) + c);
const p1 = `${p[0]}${m}`, p2 = `${m}${p[1]}`;
next.set(p1, (next.get(p1) || 0) + c);
next.set(p2, (next.get(p2) || 0) + c);
}
return [letters, next];
}
Insert cell
part1 = {
for (const s of insertions(10)) {
const [min, max] = d3.extent(countBy(s).values());
yield max - min;
}
}
Insert cell
function* insertions(n) {
let s = template;
yield s;
while (n--) yield (s = insert(s));
}
Insert cell
insert = (s) => {
const inserts = d3.pairs(s).map((p) => rules.get(p.join("")));
return d3.zip(s, [...inserts, ""]).flat().join("");
}
Insert cell
countBy = (iterable, accessor = (d) => d) =>
d3.rollup(iterable, (r) => r.length, accessor)
Insert cell
template = sections[0]
Insert cell
rules = new Map(sections[1].split("\n").map((l) => l.split(" -> ")))
Insert cell
sections = input.split("\n\n")
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