Skip to content

Commit 27d3257

Browse files
committed
2024 Day 02 part 2
1 parent 625de67 commit 27d3257

File tree

4 files changed

+39
-22
lines changed

4 files changed

+39
-22
lines changed

2024/day-02.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,5 +85,5 @@
8585
* How many reports are now safe?
8686
*/
8787

88-
//$count = $reactorReportParser->findSafeReportsCount($reports, true);
89-
//print('There are ' . $count . " safe reports after applying the Problem Dampener.\n");
88+
$count = $reactorReportParser->findSafeReportsCount($reports, true);
89+
print('There are ' . $count . " safe reports after applying the Problem Dampener.\n");

2024/src/ReactorReportParser.php

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,50 @@ public function findSafeReportsCount(array $reports, bool $applyProblemDampener
2020
foreach ($reports as $report) {
2121
$levels = explode(' ', $report);
2222

23-
$previousChange = '';
24-
$isSafeChange = true;
25-
$dampenerUsed = false;
23+
if ($this->isSafeReport($levels, $applyProblemDampener)) {
24+
$this->safeReportsCount++;
25+
}
26+
}
2627

27-
for ($i = 1; $i < count($levels); $i++) {
28-
$previousLevel = $levels[$i - 1];
29-
$currentLevel = $levels[$i];
30-
$currentChange = $previousLevel < $currentLevel ? 'increase' : 'decrease';
28+
return $this->safeReportsCount;
29+
}
3130

32-
$isSafeChange = $this->isSafeChange($previousLevel, $currentLevel, $previousChange, $currentChange);
31+
/**
32+
* @param array $levels
33+
* @param bool $applyProblemDampener
34+
* @return bool
35+
*/
36+
private function isSafeReport(array $levels, bool $applyProblemDampener = false): bool
37+
{
38+
$previousChange = '';
3339

34-
if (!$isSafeChange) {
35-
break;
36-
}
40+
for ($i = 1; $i < count($levels); $i++) {
41+
$previousLevel = $levels[$i - 1];
42+
$currentLevel = $levels[$i];
43+
$currentChange = $previousLevel < $currentLevel ? 'increase' : 'decrease';
44+
45+
$isSafeChange = $this->isSafeChange($previousLevel, $currentLevel, $previousChange, $currentChange);
3746

38-
$previousChange = $currentChange;
47+
if (!$isSafeChange && !$applyProblemDampener) {
48+
return false;
3949
}
4050

41-
if ($isSafeChange) {
42-
$this->safeReportsCount++;
51+
if (!$isSafeChange && $applyProblemDampener) {
52+
for ($j = 0; $j < count($levels); $j++) {
53+
$levelsWithoutOneLevel = $levels;
54+
array_splice($levelsWithoutOneLevel, $j, 1, []);
55+
if ($this->isSafeReport($levelsWithoutOneLevel)) {
56+
return true;
57+
}
58+
}
59+
60+
return false;
4361
}
62+
63+
$previousChange = $currentChange;
4464
}
4565

46-
return $this->safeReportsCount;
66+
return true;
4767
}
4868

4969
/**

2024/tests/ReactorReportParserTest.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,12 @@ public function testFindSafeReportsCount()
3131

3232
public function testFindSafeReportsCountUsingProblemDampener()
3333
{
34-
$this->markTestSkipped('TODO...');
35-
3634
// Day 2 Part 2 example inputs
3735
$result = $this->reactorReportParser->findSafeReportsCount($this->sampleReports, true);
3836
$this->assertEquals(4, $result);
3937

4038
// Day 2 Part 2 puzzle inputs
41-
// 685 is too low
4239
$result = $this->reactorReportParser->findSafeReportsCount($this->reports, true);
43-
$this->assertEquals(660, $result);
40+
$this->assertEquals(689, $result);
4441
}
4542
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ of changes and trends in that community.
1212
| | 2024 | 2023 | 2022 | 2021 | 2020 | 2019 | 2018 | 2017 | 2016 | 2015 |
1313
|-------:|---------------|:-------------:|:-------------:|:-------------:|:-------------:|:-------------:|:-------------:|:-------------:|:----:|:-------------:|
1414
| Day 01 | :star: :star: | :star: :star: | :star: :star: | :star: :star: | :star: :star: | :star: :star: | :star: :star: | :star: :star: | | :star: :star: |
15-
| Day 02 | :star: | :star: :star: | :star: :star: | :star: :star: | :star: :star: | :star: | :star: :star: | :star: :star: | | :star: :star: |
15+
| Day 02 | :star: :star: | :star: :star: | :star: :star: | :star: :star: | :star: :star: | :star: | :star: :star: | :star: :star: | | :star: :star: |
1616
| Day 03 | | | :star: :star: | :star: :star: | :star: :star: | | :star: :star: | | | :star: :star: |
1717
| Day 04 | | | :star: :star: | | :star: :star: | | :star: :star: | :star: :star: | | :star: :star: |
1818
| Day 05 | | | :star: :star: | | :star: :star: | | | :star: :star: | | :star: :star: |

0 commit comments

Comments
 (0)