solutions for day 7

This commit is contained in:
Eric Wagoner 2022-12-07 19:15:18 -05:00
parent b7ce003ea0
commit e95bee2b13
3 changed files with 1165 additions and 0 deletions

60
07/code.js Normal file
View File

@ -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

1082
07/input.txt Normal file

File diff suppressed because it is too large Load Diff

23
07/sample.txt Normal file
View File

@ -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