Files
AoC-2021/src/2022/day7/index.ts
2022-12-08 22:07:30 +00:00

150 lines
3.5 KiB
TypeScript

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);