Created
December 7, 2022 15:23
-
-
Save oberhamsi/c2a3ad86fca8eb1276c91228f965ba34 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// reduce function returning all directories bigger than 100K | |
const reduceToBiggerThan = (list, directory) => { | |
if (directory.size <= 100 * 1000) { | |
list.push(directory); | |
} | |
directory.children.reduce(reduceToBiggerThan, list); | |
return list; | |
} | |
// reduce to the smallest directory that is bigger than the neededSpace | |
const reduceToSmallestDelete = (neededSpace) => { | |
return function reduceToSmallest(topCandidate, directory) { | |
// too small, don't bother with children | |
if (directory.size < neededSpace) { | |
return topCandidate; | |
} | |
// go depth first | |
topCandidate = directory.children.reduce(reduceToSmallest, topCandidate); | |
if (!topCandidate || directory.size < topCandidate.size) { | |
return directory | |
} | |
return topCandidate; | |
} | |
}; | |
function Directory(parent) {; | |
this.parent = parent; | |
this.children = []; | |
this.size = 0; | |
this.addFile = (size) => { | |
this.size += size; | |
this.parent && this.parent.addFile(size); | |
} | |
return this; | |
} | |
const main = (inputString) => { | |
const topDirectory = new Directory(); | |
let currentDirectory = topDirectory; | |
inputString.split('\n').forEach(line => { | |
const parts = line.split(' '); | |
const firstWord = parts[0]; | |
const directoryName = parts[2]; | |
switch (firstWord) { | |
case '$': | |
// if directoryName is not set, it's a `ls` cmd which | |
// we ignore. we also ignore `cd /`, it happens only once at start | |
if (directoryName == '..') { | |
currentDirectory = currentDirectory.parent; | |
} else if (directoryName) { | |
// a directory is never entered twice, so we can simply create | |
// when entered | |
const newDirectory = new Directory(currentDirectory); | |
currentDirectory.children.push(newDirectory) | |
currentDirectory = newDirectory; | |
} | |
break; | |
case 'dir': | |
// dont care but don't fall into default | |
break; | |
default: | |
// must be file at at this point | |
const fileSize = parseInt(firstWord); | |
currentDirectory.addFile(fileSize); | |
} | |
}); | |
const directoriesBiggerThan = topDirectory.children.reduce(reduceToBiggerThan, []); | |
const sum = directoriesBiggerThan.reduce((prev, current) => { | |
return prev + current.size; | |
}, 0); | |
console.log('part 1', sum); | |
const usedSpace = topDirectory.size; | |
const freeSpace = 70000000 - usedSpace; | |
const neededSpace = 30000000 - freeSpace; | |
const bestDirectory = topDirectory.children.reduce(reduceToSmallestDelete(neededSpace), null); | |
console.log('part 2', bestDirectory.size) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment