52 lines
1.4 KiB
PHP
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;
|