advent_of_code_2024/day_2/part_1.php
Clement Desmidt 77cfb5eba7 Finit les part 1 du jour 2 et 3
Ouin, pas le temps de faire les part 2 😭
2024-12-03 11:20:03 +01:00

52 lines
1.4 KiB
PHP

<?php
$reports = array_filter(explode("\n", file_get_contents(__DIR__.'/input.txt')), static fn($line) =>!empty($line));
$number_of_safe_reports = 0;
function is_report_safe(string $report)
{
$levels = explode(' ', $report);
$current_number = null;
$current_way = null;
foreach ($levels as $level) {
if ($current_number === null) {
$current_number = $level;
} else {
if ($current_number === $level) {
return false;
}
if ($current_number > $level) {
if ($current_number - $level > 3) {
return false;
}
if ($current_way === null) {
$current_way = 'd';
} elseif ($current_way !== 'd') {
return false;
}
}
if ($current_number < $level) {
if ($level - $current_number > 3) {
return false;
}
if ($current_way === null) {
$current_way = 'a';
} elseif ($current_way !== 'a') {
return false;
}
}
$current_number = $level;
}
}
return true;
}
foreach ($reports as $report) {
if (is_report_safe($report)) {
$number_of_safe_reports++;
}
}
echo sprintf('Number of safe reports : %u', $number_of_safe_reports);
exit;