61 lines
1.1 KiB
JavaScript
61 lines
1.1 KiB
JavaScript
|
const fs = require('fs');
|
||
|
|
||
|
//const inputArray = fs.readFileSync('sample.txt').toString().split("\n");
|
||
|
const inputArray = fs.readFileSync('input.txt').toString().split("\n");
|
||
|
|
||
|
// Part One
|
||
|
|
||
|
const tree = { };
|
||
|
let path = [];
|
||
|
|
||
|
for (i in inputArray) {
|
||
|
const command =inputArray[i].split(' ');
|
||
|
if (command[0] === '$') {
|
||
|
if (command[1] === 'cd') {
|
||
|
if (command[2] === '..') {
|
||
|
path.pop();
|
||
|
} else {
|
||
|
path.push(command[2]);
|
||
|
tree[path.join('.')] = 0;
|
||
|
}
|
||
|
}
|
||
|
} else if (command[0] !== 'dir') {
|
||
|
const size = parseInt(command[0]);
|
||
|
const dirs = [];
|
||
|
for (const dir of path) {
|
||
|
dirs.push(dir);
|
||
|
tree[dirs.join('.')] += size;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
let summedSizes = 0;
|
||
|
for (const size of Object.values(tree)) {
|
||
|
if (size <= 100000) {
|
||
|
summedSizes += size;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
console.log(summedSizes);
|
||
|
|
||
|
|
||
|
// Part Two
|
||
|
|
||
|
const freeSpace = 70000000 - tree['/'];
|
||
|
let candidates = [];
|
||
|
|
||
|
for (const size of Object.values(tree)) {
|
||
|
if (size >= (30000000 - freeSpace)) {
|
||
|
candidates.push(size);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
candidates.sort(function(a, b) {
|
||
|
return a - b;
|
||
|
});
|
||
|
|
||
|
console.log(candidates[0]);
|
||
|
|
||
|
// functions
|
||
|
|