7.1 was a doosie
This commit is contained in:
parent
752f4aada1
commit
e10082a33d
2 changed files with 1068 additions and 0 deletions
65
2022/7/index.js
Normal file
65
2022/7/index.js
Normal file
|
@ -0,0 +1,65 @@
|
|||
|
||||
const fs = require('fs/promises');
|
||||
|
||||
const createNode = (parent) => {
|
||||
return {
|
||||
parent,
|
||||
children: {},
|
||||
localSize: 0
|
||||
};
|
||||
};
|
||||
|
||||
let answer = 0;
|
||||
|
||||
(async () => {
|
||||
const input = await fs.readFile("./input.txt", 'utf8');
|
||||
|
||||
const lines = input.split("\n");
|
||||
|
||||
const files = createNode();
|
||||
|
||||
lines.reduce((node, line) => {
|
||||
console.log("NODE: ", node);
|
||||
const cd = line.match(/^\$ cd (.*)$/);
|
||||
if(cd) {
|
||||
console.log("CD: ", cd);
|
||||
if(cd[1] == "..") {
|
||||
return node.parent;
|
||||
}
|
||||
return node.children[cd[1]];
|
||||
}
|
||||
const dir = line.match(/^dir (.*)$/);
|
||||
if(dir) {
|
||||
console.log("DIR: ", dir);
|
||||
node.children[dir[1]] = createNode(node);
|
||||
}
|
||||
const file = line.match(/^(\d+) .*$/);
|
||||
if(file) {
|
||||
node.localSize += parseFloat(file[1]);
|
||||
}
|
||||
|
||||
return node;
|
||||
|
||||
}, files);
|
||||
|
||||
console.log("FILES: ", files);
|
||||
|
||||
const fullSize = totalSmallerDirs(files);
|
||||
|
||||
console.log("FULL SIZE: ", fullSize);
|
||||
console.log("ANSWER: ", answer);
|
||||
|
||||
})();
|
||||
|
||||
const totalSmallerDirs = (node) => {
|
||||
let totalSize = node.localSize;
|
||||
|
||||
totalSize += sum(Object.values(node.children).map(totalSmallerDirs));
|
||||
|
||||
if(totalSize <= 100000) {
|
||||
answer += totalSize;
|
||||
}
|
||||
return totalSize;
|
||||
}
|
||||
|
||||
const sum = (arr) => arr.reduce((total, num) => total+num, 0);
|
1003
2022/7/input.txt
Normal file
1003
2022/7/input.txt
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue