Compare commits
No commits in common. "0e77c0ae39a24c38500854d1729787c49482d56f" and "752f4aada118f8a5481c6b3c48c74ffe015f0e37" have entirely different histories.
0e77c0ae39
...
752f4aada1
3 changed files with 0 additions and 1143 deletions
|
@ -1,65 +0,0 @@
|
||||||
|
|
||||||
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);
|
|
|
@ -1,75 +0,0 @@
|
||||||
|
|
||||||
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) => {
|
|
||||||
const cd = line.match(/^\$ cd (.*)$/);
|
|
||||||
if(cd) {
|
|
||||||
if(cd[1] == "..") {
|
|
||||||
return node.parent;
|
|
||||||
}
|
|
||||||
return node.children[cd[1]];
|
|
||||||
}
|
|
||||||
const dir = line.match(/^dir (.*)$/);
|
|
||||||
if(dir) {
|
|
||||||
node.children[dir[1]] = createNode(node);
|
|
||||||
}
|
|
||||||
const file = line.match(/^(\d+) .*$/);
|
|
||||||
if(file) {
|
|
||||||
node.localSize += parseFloat(file[1]);
|
|
||||||
}
|
|
||||||
|
|
||||||
return node;
|
|
||||||
|
|
||||||
}, files);
|
|
||||||
|
|
||||||
|
|
||||||
const fullSize = totalSmallerDirs(files);
|
|
||||||
|
|
||||||
console.log("FULL SIZE: ", fullSize);
|
|
||||||
|
|
||||||
console.log("SPACE NEEDED: ", fullSize - 40000000);
|
|
||||||
|
|
||||||
const [_, bestDeletionCandidate] = findBestDeletionCandidate(files, fullSize - 40000000);
|
|
||||||
console.log("Best candidate for deletion is of size: ", bestDeletionCandidate);
|
|
||||||
})();
|
|
||||||
|
|
||||||
const totalSmallerDirs = (node) => {
|
|
||||||
let totalSize = node.localSize;
|
|
||||||
|
|
||||||
totalSize += sum(Object.values(node.children).map(totalSmallerDirs));
|
|
||||||
|
|
||||||
if(totalSize <= 100000) {
|
|
||||||
answer += totalSize;
|
|
||||||
}
|
|
||||||
return totalSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
const findBestDeletionCandidate = (node, spaceNeeded) => {
|
|
||||||
let totalSize = node.localSize;
|
|
||||||
|
|
||||||
const childrenResults = Object.values(node.children).map(child => findBestDeletionCandidate(child, spaceNeeded));
|
|
||||||
totalSize += sum(childrenResults.map(([x]) => x));
|
|
||||||
const deletionCandidates = childrenResults.map(([_, y]) => y);
|
|
||||||
deletionCandidates.push(totalSize);
|
|
||||||
console.log("Deletion Candidates: ", deletionCandidates);
|
|
||||||
|
|
||||||
return [totalSize, deletionCandidates.reduce((best, current) => current < best && current >= spaceNeeded ? current : best, Infinity)];
|
|
||||||
}
|
|
||||||
const sum = (arr) => arr.reduce((total, num) => total+num, 0);
|
|
1003
2022/7/input.txt
1003
2022/7/input.txt
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue