File tree Expand file tree Collapse file tree 1 file changed +30
-16
lines changed Expand file tree Collapse file tree 1 file changed +30
-16
lines changed Original file line number Diff line number Diff line change 5
5
class Day7 extends AbstractDay
6
6
{
7
7
public function solve_part_1 (): string
8
+ {
9
+ $ dirSizes = $ this ->getDirSizes ();
10
+ $ totalSize = 0 ;
11
+ $ maxSize = 100000 ;
12
+ foreach ($ dirSizes as $ dir => $ size ) {
13
+ if ($ size <= $ maxSize ) {
14
+ $ totalSize += $ size ;
15
+ }
16
+ }
17
+ return $ totalSize ;
18
+ }
19
+
20
+ public function solve_part_2 (): string
21
+ {
22
+ $ dirSizes = $ this ->getDirSizes ();
8000
23
+ $ totalSize = $ smallestDirSize = 70000000 ;
24
+ $ neededFreeSize = 30000000 ;
25
+ $ currentUsedSize = $ dirSizes ['/ ' ];
26
+ foreach ($ dirSizes as $ dir => $ size ) {
27
+ if ($ totalSize - $ currentUsedSize + $ size >= $ neededFreeSize ) {
28
+ if ($ size < $ smallestDirSize ) {
29
+ $ smallestDirSize = $ size ;
30
+ }
31
+ }
32
+ }
33
+ return $ smallestDirSize ;
34
+ }
35
+
36
+ protected function getDirSizes ()
8
37
{
9
38
$ filesystem = $ this ->getFilesystem ();
10
39
$ dirSizes = [];
11
- $ totalSize = 0 ;
12
40
foreach ($ filesystem as $ path => $ size ) {
13
41
if (str_ends_with ($ path , '/ ' )) {
14
42
continue ;
@@ -24,22 +52,8 @@ public function solve_part_1(): string
24
52
break ;
25
53
}
26
54
}
27
-
28
55
}
29
-
30
- $ maxSize = 100000 ;
31
- foreach ($ dirSizes as $ dir => $ size ) {
32
- if ($ size <= $ maxSize ) {
33
- $ totalSize += $ size ;
34
- }
35
- }
36
-
37
- return $ totalSize ;
38
- }
39
-
40
- public function solve_part_2 (): string
41
- {
42
- return "TODO " ;
56
+ return $ dirSizes ;
43
57
}
44
58
45
59
protected function getFilesystem ()
You can’t perform that action at this time.
0 commit comments