Public
Edited
Dec 17, 2022
Insert cell
Insert cell
Insert cell
listToTree(cmdList)
Insert cell
function listToTree(cmdList) {
const tree = {};

let curr = tree;

cmdList.forEach((cmd) => {
if (cmd.type === "cd" && cmd.name !== "..") {
curr.name = curr.name ?? cmd.name;
}

if (cmd.type === "ls") {
curr.children = cmd.children.map((child) => {
return child.type === "file"
? { name: child.name, size: child.size }
: { name: child.name, children: [] };
});
}
});

return tree;
}
Insert cell
tree1 = ({
name: "a",
children: [
{ name: "ab" },
{ name: "ac" },
{ name: "ad", children: [{ name: "ada" }] }
]
})
Insert cell
walk(tree1, (node) => ({ ...node, _visited: true }))
Insert cell
Insert cell
cmdList = parseInput(sampleInput)
Insert cell
tree = buildTree(cmdList)
Insert cell
Insert cell
Insert cell
Insert cell
import { NEWLINE, SPACE } from "@pnavarrc/advent-of-code-utils"
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