Public
Edited
Dec 8, 2022
Insert cell
Insert cell
Insert cell
function getDirSize(dir) {
let filesSize = _.sumBy(dir.files, (filename) => dataset.files.get(filename));
let subdirsSize = _.sumBy(dir.subdirs, (dirname) =>
getDirSize(dataset.dirs.get(dirname))
);
return filesSize + subdirsSize;
}
Insert cell
dirsTotalSize = Array.from(dataset.dirs.keys()).map((fullpath) => ({
name: fullpath,
totalSize: getDirSize(dataset.dirs.get(fullpath)),
dir: dataset.dirs.get(fullpath)
}))
Insert cell
below100k = dirsTotalSize.filter(d => d.totalSize <= 100000)
Insert cell
sumOfBelow100k = _.sumBy(below100k, (d) => d.totalSize)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
toDelete = _.minBy(
dirsTotalSize.filter((d) => d.totalSize >= spaceToFind),
(d) => d.totalSize
)
Insert cell
Insert cell
function readInput(txt) {
let dirs = new Map([
[
"/",
{ parent: undefined, subdirs: [], files: [], name: "/", fullpath: "/" }
]
]);
let files = new Map();

let currentDir = "";
let pathToCurrentDir = [];
for (let line of txt.split("\n").filter((l) => l.trim().length > 0)) {
switch (line[0]) {
case "$": // command, do nothing on ls
if (line.slice(2, 4) === "cd") {
let [_, cmd, target] = line.split(" ");
if (target === "..") {
if (pathToCurrentDir.length > 0)
currentDir = pathToCurrentDir.pop();
} else {
let targetFullPath = [...pathToCurrentDir, target].join("/");
let targetDir = dirs.get(targetFullPath);
if (targetDir === undefined) {
dirs.set(targetFullPath, {
parent: currentDir,
subdirs: [],
files: [],
name: target,
fullpath: targetFullPath
});
dirs.get(pathToCurrentDir.join("/")).subdirs.push(targetFullPath);
}
pathToCurrentDir.push(target);
currentDir = target;
}
}
break;
case "d": // directory
let [keyword, dirname] = line.split(" ");
let dirFullPath = [...pathToCurrentDir, dirname].join("/");
if (dirs.get(dirFullPath) === undefined) {
dirs.set(dirFullPath, {
parent: currentDir,
subdirs: [],
files: [],
name: dirname,
fullpath: dirFullPath
});
dirs.get(pathToCurrentDir.join("/")).subdirs.push(dirFullPath);
}
break;
default: // file
let [size, filename] = line.split(" ");
let filepath = [...pathToCurrentDir, filename].join("/");
if (files.get(filename) === undefined) {
files.set(filepath, parseInt(size));
dirs.get(pathToCurrentDir.join("/")).files.push(filepath);
}
}
}

return {
dirs: dirs,
files: files
};
}
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