Notebooks 2.0 is here.

Public
Edited
Dec 12, 2022
1 fork
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
lines = input.trim().split('\n')
Insert cell
vfs = visit(stripParent, R.identity, buildTree(lines))
Insert cell
vfsWithSizes = visit(R.identity, addSize, vfs)
Insert cell
sumOfSmallDirectories = {
let total = 0

visit(R.tap(node => {
if (node?.size <= 100000) {
total += node.size
}
}), R.identity, vfsWithSizes)

return total
}
Insert cell
Insert cell
smallestDirectoryThatFreesUpEnoughSpace = {
const target = 30_000_000

const getFreeSpaceAfterDeletion = node => 70_000_000 - vfsWithSizes['/'].size + node.size
const getOvershoot = node => getFreeSpaceAfterDeletion(node) - target
let choice = vfsWithSizes['/']

visit(R.tap(node => {
const overshoot = getOvershoot(node)
if (overshoot > 0 && overshoot < getOvershoot(choice)) {
choice = node
}
}), R.identity, vfsWithSizes)

return {choice, overshoot: getOvershoot(choice)}
}
Insert cell
Insert cell
onlyForObjects = R.curry((f, node) => typeof node === 'object' ? f(node) : node)
Insert cell
stripParent = onlyForObjects(({parent, ...rest}) => rest)
Insert cell
getSize = node => typeof node === 'number' ? node : R.sum(Object.values(node).map(x => typeof x === 'number' ? x : x.size ?? 0))
Insert cell
addSize = onlyForObjects(node => ({...node, size: getSize(node)}))
Insert cell
regex = /(?:\$ (?<command>ls|cd)(?: (?<argument>.+))?)|(dir (?<directoryName>\w+))|((?<size>\d+) (?<fileName>\w+))/
Insert cell
buildTree = lines => {
const vfs = {}
let current
let currentCommand

for (const line of lines) {
const {groups} = regex.exec(line)

console.log({current, currentCommand, groups})

if (groups.command) {
currentCommand = groups.command

switch (groups.command) {
case 'ls': continue
case 'cd': {
switch (groups.argument.trim()) {
case '..': {
current = current.parent
break
}
case '/': {
vfs['/'] ??= {}
current = vfs['/']
break
}
default: {
current[groups.argument] ??= {parent: current}
current = current[groups.argument]
break
}
}
break
}
}

continue
}

if (currentCommand === 'ls') {
if (groups.directoryName) {
current[groups.directoryName] ??= {parent: current}
}
if (groups.fileName) {
current[groups.fileName] ??= parseInt(groups.size)
}
}
}

return vfs;
}
Insert cell
visit = {
const visitInternal = R.curry((beforeRecursionFunc, afterRecursionFunc, tree) => {
if (!tree) return
const processedTree = {}
Object.entries(tree).forEach(([key, node]) => {
const processedNode = beforeRecursionFunc(node)
if (processedNode) {
processedTree[key] = typeof processedNode === 'object'
? afterRecursionFunc(visitInternal(beforeRecursionFunc, afterRecursionFunc, processedNode))
: processedNode
}
})
return processedTree
})

return visitInternal
}
Insert cell
import {adventOfCodeHeader, renderInput, R} from '@senritsu/advent-of-code-2022'
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