Compare commits

...

2 commits

Author SHA1 Message Date
John Shaver
0e77c0ae39 7.2 was easier, at least 2022-12-07 08:12:13 -08:00
John Shaver
e10082a33d 7.1 was a doosie 2022-12-07 07:36:06 -08:00
3 changed files with 1143 additions and 0 deletions

65
2022/7/index.js Normal file
View 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);

75
2022/7/index2.js Normal file
View file

@ -0,0 +1,75 @@
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 Normal file

File diff suppressed because it is too large Load diff