Skip to content

Commit 3c3a8f4

Browse files
committed
Solution 2024 day 2
1 parent f505919 commit 3c3a8f4

File tree

4 files changed

+1129
-0
lines changed

4 files changed

+1129
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
## --- Day 2: Red-Nosed Reports ---
2+
3+
Fortunately, the first location The Historians want to search isn't a long walk from the Chief Historian's office.
4+
5+
While the Red-Nosed Reindeer nuclear fusion/fission plant appears to contain no sign of the Chief Historian, the engineers there run up to you as soon as they see you. Apparently, they still talk about the time Rudolph was saved through molecular synthesis from a single electron.
6+
7+
They're quick to add that - since you're already here - they'd really appreciate your help analyzing some unusual data from the Red-Nosed reactor. You turn to check if The Historians are waiting for you, but they seem to have already divided into groups that are currently searching every corner of the facility. You offer to help with the unusual data.
8+
9+
The unusual data (your puzzle input) consists of many reports, one report per line. Each report is a list of numbers called levels that are separated by spaces. For example:
10+
11+
7 6 4 2 1
12+
1 2 7 8 9
13+
9 7 6 2 1
14+
1 3 2 4 5
15+
8 6 4 4 1
16+
1 3 6 7 9
17+
18+
This example data contains six reports each containing five levels.
19+
20+
The engineers are trying to figure out which reports are safe. The Red-Nosed reactor safety systems can only tolerate levels that are either gradually increasing or gradually decreasing. So, a report only counts as safe if both of the following are true:
21+
22+
- The levels are either all increasing or all decreasing.
23+
- Any two adjacent levels differ by at least one and at most three.
24+
25+
In the example above, the reports can be found safe or unsafe by checking those rules:
26+
27+
- ```7 6 4 2 1```: Safe because the levels are all decreasing by ```1``` or ```2```.
28+
- ```1 2 7 8 9```: Unsafe because ```2 7``` is an increase of ```5```.
29+
- ```9 7 6 2 1```: Unsafe because ```6 2``` is a decrease of ```4```.
30+
- ```1 3 2 4 5```: Unsafe because ```1 3``` is increasing but ```3 2``` is decreasing.
31+
- ```8 6 4 4 1```: Unsafe because ```4 4``` is neither an increase or a decrease.
32+
- ```1 3 6 7 9```: Safe because the levels are all increasing by ```1```, ```2```, or ```3```.
33+
34+
So, in this example, ```2``` reports are safe.
35+
36+
Analyze the unusual data from the engineers. How many reports are safe?
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
namespace App\AdventSolutions\Year2024\Day2;
4+
5+
use App\AdventSolutions\AbstractSolution;
6+
7+
class Solution2024Day2 extends AbstractSolution
8+
{
9+
public function solvePart1($input, $debug = false): string
10+
{
11+
$safeReports = 0;
12+
13+
foreach ($input as $line) {
14+
if ($this->isSafe($this->getLevels($line))) {
15+
$safeReports++;
16+
}
17+
}
18+
19+
return "Number of safe reports: <info>$safeReports</info>";
20+
}
21+
22+
public function solvePart2($input, $debug = false): string
23+
{
24+
$safeReports = 0;
25+
26+
foreach ($input as $line) {
27+
if ($this->isSafe($this->getLevels($line), 1)) {
28+
$safeReports++;
29+
}
30+
}
31+
32+
return "Number of safe reports (with dampeners): <info>$safeReports</info>";
33+
}
34+
35+
private function getLevels($line)
36+
{
37+
return explode(" ", $line);
38+
}
39+
40+
private function isSafe($levels, $ignoreLevels = 0): bool
41+
{
42+
if (count($levels) < 2) {
43+
return true; // A single level or empty array is trivially safe
44+
}
45+
46+
// Helper function to validate the sequence
47+
$isValid = function ($levels) {
48+
return $this->isValid($levels);
49+
};
50+
51+
// If no levels are to be ignored, check the sequence directly
52+
if ($ignoreLevels === 0) {
53+
return $isValid($levels);
54+
}
55+
56+
// Try ignoring up to $ignoreLevels and check if the sequence is valid
57+
foreach ($levels as $index => $level) {
58+
$modified = $levels;
59+
unset($modified[$index]); // Ignore one level
60+
$modified = array_values($modified); // Re-index the array
61+
62+
if ($this->isSafe($modified, $ignoreLevels - 1)) {
63+
return true; // Sequence is safe by ignoring up to the specified levels
64+
}
65+
}
66+
67+
return false; // No valid sequence found
68+
}
69+
70+
private function isValid($levels): bool
71+
{
72+
$isIncreasing = $levels[1] > $levels[0];
73+
for ($i = 0; $i < count($levels) - 1; $i++) {
74+
$diff = $levels[$i + 1] - $levels[$i];
75+
if ($isIncreasing) {
76+
if ($diff <= 0 || $diff > 3) {
77+
return false;
78+
}
79+
} else {
80+
if ($diff >= 0 || $diff < -3) {
81+
return false;
82+
}
83+
}
84+
}
85+
return true;
86+
}
87+
}

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