@@ -14,6 +14,7 @@ class Dir {
14
14
parent : Dir | undefined
15
15
childrenDir : Dir [ ] = [ ]
16
16
files : File [ ] = [ ]
17
+ totalSize = 0
17
18
constructor ( name : string , parent : Dir | undefined ) {
18
19
this . name = name
19
20
this . parent = parent
@@ -31,17 +32,22 @@ class Dir {
31
32
if ( ! file ) {
32
33
file = new File ( name , size )
33
34
this . files . push ( file )
35
+ this . totalSize += size
34
36
}
35
37
}
38
+ updateTotalSize = ( subDir : Dir ) => {
39
+ //console.log('update', this.name, '<-', subDir.name, subDir.totalSize)
40
+ this . totalSize += subDir . totalSize
41
+ }
36
42
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 ( ", " ) )
38
44
}
39
45
}
40
46
41
47
class Solve07 extends FileReader {
42
48
constructor ( ) {
43
49
super ( ) ;
44
- this . readData ( "src/07/test .data" )
50
+ this . readData ( "src/07/input .data" )
45
51
. then ( ( data ) => {
46
52
this . process ( data . split ( "\n" ) ) ;
47
53
} )
@@ -54,6 +60,23 @@ class Solve07 extends FileReader {
54
60
dir . childrenDir . forEach ( d => this . printTree ( d , level * 2 ) )
55
61
}
56
62
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
+
57
80
process = ( data : string [ ] ) => {
58
81
let root : Dir = new Dir ( "/" , undefined )
59
82
let parent : Dir | undefined = undefined
@@ -97,7 +120,10 @@ class Solve07 extends FileReader {
97
120
list = true
98
121
}
99
122
}
100
- this . printTree ( root , 2 )
123
+ this . sumFiles ( root )
124
+ //this.printTree(root, 2)
125
+ this . totalSumCalc ( root )
126
+ console . log ( this . totalSum1 )
101
127
} ;
102
128
}
103
129
0 commit comments