Day 07
This commit is contained in:
parent
ed6a91564f
commit
54406d8b4f
4 changed files with 1091 additions and 1 deletions
|
@ -30,6 +30,7 @@
|
|||
"Day03": "cd ./src/03_RucksackReorganization && deno run --allow-read=./input.txt RucksackReorganization.ts",
|
||||
"Day04": "cd ./src/04_CampCleanup && deno run --allow-read=./input.txt CampCleanup.ts",
|
||||
"Day05": "cd ./src/05_SupplyStacks && deno run --allow-read=./input.txt SupplyStacks.ts",
|
||||
"Day06": "cd ./src/06_TuningTrouble && deno run --allow-read=./input.txt TuningTrouble.ts"
|
||||
"Day06": "cd ./src/06_TuningTrouble && deno run --allow-read=./input.txt TuningTrouble.ts",
|
||||
"Day07": "cd ./src/07_NoSpaceLeftOnDevice && deno run --allow-read=./input.txt NoSpaceLeftOnDevice.ts"
|
||||
}
|
||||
}
|
||||
|
|
16
deno.lock
Normal file
16
deno.lock
Normal file
|
@ -0,0 +1,16 @@
|
|||
{
|
||||
"version": "2",
|
||||
"remote": {
|
||||
"https://deno.land/std@0.167.0/_util/asserts.ts": "d0844e9b62510f89ce1f9878b046f6a57bf88f208a10304aab50efcb48365272",
|
||||
"https://deno.land/std@0.167.0/_util/os.ts": "8a33345f74990e627b9dfe2de9b040004b08ea5146c7c9e8fe9a29070d193934",
|
||||
"https://deno.land/std@0.167.0/path/_constants.ts": "df1db3ffa6dd6d1252cc9617e5d72165cd2483df90e93833e13580687b6083c3",
|
||||
"https://deno.land/std@0.167.0/path/_interface.ts": "ee3b431a336b80cf445441109d089b70d87d5e248f4f90ff906820889ecf8d09",
|
||||
"https://deno.land/std@0.167.0/path/_util.ts": "d16be2a16e1204b65f9d0dfc54a9bc472cafe5f4a190b3c8471ec2016ccd1677",
|
||||
"https://deno.land/std@0.167.0/path/common.ts": "bee563630abd2d97f99d83c96c2fa0cca7cee103e8cb4e7699ec4d5db7bd2633",
|
||||
"https://deno.land/std@0.167.0/path/glob.ts": "81cc6c72be002cd546c7a22d1f263f82f63f37fe0035d9726aa96fc8f6e4afa1",
|
||||
"https://deno.land/std@0.167.0/path/mod.ts": "cf7cec7ac11b7048bb66af8ae03513e66595c279c65cfa12bfc07d9599608b78",
|
||||
"https://deno.land/std@0.167.0/path/posix.ts": "b859684bc4d80edfd4cad0a82371b50c716330bed51143d6dcdbe59e6278b30c",
|
||||
"https://deno.land/std@0.167.0/path/separator.ts": "fe1816cb765a8068afb3e8f13ad272351c85cbc739af56dacfc7d93d710fe0f9",
|
||||
"https://deno.land/std@0.167.0/path/win32.ts": "7cebd2bda6657371adc00061a1d23fdd87bcdf64b4843bb148b0b24c11b40f69"
|
||||
}
|
||||
}
|
59
src/07_NoSpaceLeftOnDevice/NoSpaceLeftOnDevice.ts
Normal file
59
src/07_NoSpaceLeftOnDevice/NoSpaceLeftOnDevice.ts
Normal file
|
@ -0,0 +1,59 @@
|
|||
import { join } from 'https://deno.land/std@0.167.0/path/mod.ts';
|
||||
|
||||
type Directory = { type: 'directory' };
|
||||
type File = { type: 'file'; size: number };
|
||||
type Tree = Record<string, Record<string, Directory | File>>;
|
||||
|
||||
function buildTree(shellLog: string): Tree {
|
||||
const tree: Tree = {};
|
||||
let curPath = '';
|
||||
for (const line of shellLog.split('\n')) {
|
||||
if (line.startsWith('$')) {
|
||||
if (line === '$ ls') {
|
||||
continue;
|
||||
}
|
||||
curPath = join(curPath, line.split(' ').at(-1)!);
|
||||
tree[curPath] ??= {};
|
||||
continue;
|
||||
}
|
||||
if (line.startsWith('dir')) {
|
||||
const dirName = line.split(' ').at(-1)!;
|
||||
tree[curPath][dirName] = {
|
||||
type: 'directory',
|
||||
};
|
||||
continue;
|
||||
}
|
||||
// only option left is file
|
||||
const [fileSize, fileName] = line.split(' ') as [string, string];
|
||||
tree[curPath][fileName] = {
|
||||
type: 'file',
|
||||
size: parseInt(fileSize),
|
||||
};
|
||||
}
|
||||
return tree;
|
||||
}
|
||||
|
||||
function duRec(tree: Tree, entryPath = '/'): Record<string, number> {
|
||||
let result: Record<string, number> = {};
|
||||
let curSize = 0;
|
||||
for (const [name, entry] of Object.entries(tree[entryPath as keyof typeof tree])) {
|
||||
if (entry.type === 'directory') {
|
||||
const newEntry = join(entryPath, name);
|
||||
result = Object.assign(result, duRec(tree, newEntry)) as Record<string, number>;
|
||||
curSize += result[newEntry];
|
||||
} else curSize += entry.size;
|
||||
}
|
||||
result[entryPath] = curSize;
|
||||
return result;
|
||||
}
|
||||
|
||||
const input = Deno.readTextFileSync('./input.txt').trimEnd();
|
||||
const tree = buildTree(input);
|
||||
const dirSizes = Object.values(duRec(tree));
|
||||
const p1 = dirSizes.filter((size) => (size <= 100000))
|
||||
.reduce((prev, cur) => prev + cur, 0);
|
||||
console.log('Solution Part 1:', p1);
|
||||
dirSizes.sort((a, b) => a - b);
|
||||
const free = 70000000 - dirSizes.at(-1)!;
|
||||
const p2 = dirSizes.find((size) => (free + size) >= 30000000);
|
||||
console.log('Solution Part 2:', p2);
|
1014
src/07_NoSpaceLeftOnDevice/input.txt
Normal file
1014
src/07_NoSpaceLeftOnDevice/input.txt
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue