parent
b7ce003ea0
commit
e95bee2b13
@ -0,0 +1,60 @@ |
||||
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
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,23 @@ |
||||
$ cd / |
||||
$ ls |
||||
dir a |
||||
14848514 b.txt |
||||
8504156 c.dat |
||||
dir d |
||||
$ cd a |
||||
$ ls |
||||
dir e |
||||
29116 f |
||||
2557 g |
||||
62596 h.lst |
||||
$ cd e |
||||
$ ls |
||||
584 i |
||||
$ cd .. |
||||
$ cd .. |
||||
$ cd d |
||||
$ ls |
||||
4060174 j |
||||
8033020 d.log |
||||
5626152 d.ext |
||||
7214296 k |
Loading…
Reference in new issue