day7
This commit is contained in:
@@ -9,6 +9,6 @@
|
|||||||
"typescript": "^4.9.3"
|
"typescript": "^4.9.3"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "nodemon src/2022/day6/index.ts"
|
"start": "nodemon src/2022/day7/index.ts"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
23
src/2022/day7/example.txt
Normal file
23
src/2022/day7/example.txt
Normal 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
|
||||||
149
src/2022/day7/index.ts
Normal file
149
src/2022/day7/index.ts
Normal file
@@ -0,0 +1,149 @@
|
|||||||
|
import path from 'path';
|
||||||
|
import { readFile, sum } from "../../utils";
|
||||||
|
|
||||||
|
let input = readFile(path.resolve(__dirname, 'input.txt'));
|
||||||
|
|
||||||
|
class ElFile {
|
||||||
|
|
||||||
|
public name: string;
|
||||||
|
public size: number;
|
||||||
|
|
||||||
|
constructor(name: string, size: number) {
|
||||||
|
this.name = name;
|
||||||
|
this.size = size;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class NodElf {
|
||||||
|
|
||||||
|
public path: string;
|
||||||
|
public parent?: NodElf;
|
||||||
|
public children: NodElf[] = [];
|
||||||
|
public files: ElFile[] = [];
|
||||||
|
|
||||||
|
constructor(path: string, parent: NodElf) {
|
||||||
|
this.path = path;
|
||||||
|
this.parent = parent;
|
||||||
|
}
|
||||||
|
|
||||||
|
public goTo(arg: string): NodElf | undefined {
|
||||||
|
|
||||||
|
if (arg === "..") {
|
||||||
|
if (this.parent)
|
||||||
|
return this.parent;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(arg in this.getFolders())) {
|
||||||
|
const newNode = new NodElf(arg, this);
|
||||||
|
this.children.push(newNode)
|
||||||
|
return newNode;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public addFolder(path: string) {
|
||||||
|
if (!(path in this.getFolders())) {
|
||||||
|
const newNode = new NodElf(path, this);
|
||||||
|
this.children.push(newNode)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public addFile(size: string, name: string) {
|
||||||
|
if (!(name in this.getFiles())) {
|
||||||
|
const newFile = new ElFile(name, Number(size));
|
||||||
|
this.files.push(newFile);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public getFiles() {
|
||||||
|
return this.files.map(file => file.name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public getFolders() {
|
||||||
|
return this.children.map(node => node.path);
|
||||||
|
}
|
||||||
|
|
||||||
|
public getAllFolders() {
|
||||||
|
return this.children.concat(this.children.flatMap(child => child.getAllFolders()));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public getSize(): number {
|
||||||
|
return this.children.reduce((a, b) => a + b.getSize(), 0) + this.files.reduce((a, b) => a + b.size, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class ExtendedLeetFilesystem {
|
||||||
|
|
||||||
|
public root: NodElf;
|
||||||
|
public currentDir: NodElf | undefined;
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
this.root = new NodElf('/', null);
|
||||||
|
this.currentDir = this.root;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public cmd(command: string, arg: string) {
|
||||||
|
if (command === "cd") {
|
||||||
|
if (arg === "/" && this.currentDir?.path === "/") return;
|
||||||
|
this.currentDir = this.currentDir?.goTo(arg);
|
||||||
|
}
|
||||||
|
else if (command === "ls") {
|
||||||
|
//console.log("Print content of", this.currentDir?.path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public output(type: string, arg: string) {
|
||||||
|
if (type === "dir") {
|
||||||
|
this.currentDir?.addFolder(arg)
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
this.currentDir?.addFile(type, arg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public getDirectory(path: string) {
|
||||||
|
const dirs = path.split("/");
|
||||||
|
return dirs.reduce((a, b, c) => a, this.currentDir);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
const inputLines = input.split("\n");
|
||||||
|
let currentFolder = [];
|
||||||
|
|
||||||
|
const ELF: ExtendedLeetFilesystem = new ExtendedLeetFilesystem();
|
||||||
|
|
||||||
|
for (let idx = 0; idx < inputLines.length; idx++) {
|
||||||
|
|
||||||
|
const line = inputLines[idx];
|
||||||
|
|
||||||
|
if (line.startsWith("$")) {
|
||||||
|
const [dollah, cmd, path] = line.split(" ");
|
||||||
|
ELF.cmd(cmd, path);
|
||||||
|
}
|
||||||
|
|
||||||
|
else {
|
||||||
|
const [type, arg] = line.split(" ");
|
||||||
|
ELF.output(type, arg);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
const maxSize = 100000;
|
||||||
|
const total = ELF.root.getAllFolders().filter(node => node.getSize() < maxSize).map(node => node.getSize()).reduce(sum);
|
||||||
|
console.log("Part 1", total)
|
||||||
|
|
||||||
|
|
||||||
|
const sizeAvailable = 70000000;
|
||||||
|
const spaceNeeded = 30000000;
|
||||||
|
const currentSize = ELF.root.getSize();
|
||||||
|
const toDelete = spaceNeeded - (sizeAvailable - currentSize);
|
||||||
|
|
||||||
|
const total2 = ELF.root.getAllFolders().sort((a, b) => a.getSize() - b.getSize()).filter(node => node.getSize() > toDelete)[0].getSize();
|
||||||
|
|
||||||
|
console.log("Part2:", total2);
|
||||||
|
|
||||||
1105
src/2022/day7/input.txt
Normal file
1105
src/2022/day7/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user