Skip to content

Commit e439ca4

Browse files
committed
07 part1
1 parent 224f102 commit e439ca4

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

src/07/solve.ts

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class Dir {
1414
parent: Dir | undefined
1515
childrenDir: Dir[] = []
1616
files: File[] = []
17+
totalSize = 0
1718
constructor(name: string, parent: Dir | undefined) {
1819
this.name = name
1920
this.parent = parent
@@ -31,17 +32,22 @@ class Dir {
3132
if (!file) {
3233
file = new File(name, size)
3334
this.files.push(file)
35+
this.totalSize += size
3436
}
3537
}
38+
updateTotalSize = (subDir: Dir) => {
39+
//console.log('update', this.name, '<-', subDir.name, subDir.totalSize)
40+
this.totalSize += subDir.totalSize
41+
}
3642
print = (prefix: string = "") => {
37-
console.log(prefix, "Dir:", this.name, "parent:", this.parent?.name, "children:", this.childrenDir.map(c => c.name).join(", "), "files:", this.files.map(f => f.name).join(", "))
43+
console.log(prefix, "Dir:", this.name, this.totalSize, "p:", this.parent?.name, "sub:", this.childrenDir.map(c => c.name).join(", "), "files:", this.files.map(f => f.name).join(", "))
3844
}
3945
}
4046

4147
class Solve07 extends FileReader {
4248
constructor() {
4349
super();
44-
this.readData("src/07/test.data")
50+
this.readData("src/07/input.data")
4551
.then((data) => {
4652
this.process(data.split("\n"));
4753
})
@@ -54,6 +60,23 @@ class Solve07 extends FileReader {
5460
dir.childrenDir.forEach(d => this.printTree(d, level * 2))
5561
}
5662

63+
sizeLimit1 = 100000
64+
totalSum1 = 0
65+
totalSumCalc = (dir: Dir): void => {
66+
if (dir.totalSize <= this.sizeLimit1) {
67+
console.log('sum', dir.name, dir.totalSize)
68+
this.totalSum1 += dir.totalSize
69+
}
70+
dir.childrenDir.forEach(d => this.totalSumCalc(d))
71+
}
72+
73+
sumFiles = (dir: Dir): void => {
74+
dir.childrenDir.forEach(d => {
75+
this.sumFiles(d)
76+
})
77+
dir.parent?.updateTotalSize(dir)
78+
}
79+
5780
process = (data: string[]) => {
5881
let root: Dir = new Dir("/", undefined)
5982
let parent: Dir | undefined = undefined
@@ -97,7 +120,10 @@ class Solve07 extends FileReader {
97120
list = true
98121
}
99122
}
100-
this.printTree(root, 2)
123+
this.sumFiles(root)
124+
//this.printTree(root, 2)
125+
this.totalSumCalc(root)
126+
console.log(this.totalSum1)
101127
};
102128
}
103129

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy