Published
Edited
Dec 11, 2019
Insert cell
Insert cell
Insert cell
parseOrbits = x => x.split("\n").map(z => z.split(")"))
Insert cell
// from list of 2d arrays, makes [{id: "COM"}, {id: "B", parentId: "COM"}, ...]
// key names are what the default accesors for d3.stratify are looking for
prepOrbits = root => d => [{id: root}].concat(d.map(z => ({id: z[1], parentId: z[0]})))
Insert cell
orbitTree = d => d3.stratify()(prepOrbits("COM")(parseOrbits(d)))
Insert cell
solve1 = x =>
orbitTree(x)
.descendants() // all nodes
.reduce((acc, inc) => acc + inc.depth, 0) // sum of all their depths
Insert cell
solve1(testdata)
Insert cell
solve1(data)
Insert cell
Insert cell
jumps = d => (p1, p2) => {
const tree = orbitTree(d)
const start = tree.leaves().find(z => z.id==p1)
const end = tree.leaves().find(z => z.id==p2)
return start.path(end)
}
Insert cell
solve2 = d => jumps(d)("YOU", "SAN").length - 3 // exclude yourself, santa, orbit santa's parent
Insert cell
solve2(testdata2)
Insert cell
solve2(data)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
d3 = require("d3")
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