8000 Day 7, part2 · FailedCode/adventofcode-2022-php@b7d1ed3 · GitHub
[go: up one dir, main page]

Skip to content

Commit b7d1ed3

Browse files
committed
Day 7, part2
1 parent 51567b1 commit b7d1ed3

File tree

1 file changed

+30
-16
lines changed

1 file changed

+30
-16
lines changed

src/Days/Day7.php

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,38 @@
55
class Day7 extends AbstractDay
66
{
77
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()
837
{
938
$filesystem = $this->getFilesystem();
1039
$dirSizes = [];
11-
$totalSize = 0;
1240
foreach ($filesystem as $path => $size) {
1341
if (str_ends_with($path, '/')) {
1442
continue;
@@ -24,22 +52,8 @@ public function solve_part_1(): string
2452
break;
2553
}
2654
}
27-
2855
}
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;
4357
}
4458

4559
protected function getFilesystem()

0 commit comments

Comments
 (0)
0