37 lines
856 B
PHP
37 lines
856 B
PHP
<?php
|
|
|
|
$intervals = array_map(fn($interval) => explode('-', $interval), explode(',', file_get_contents('./input.txt')));
|
|
|
|
$invalids = [];
|
|
|
|
foreach ($intervals as $interval) {
|
|
[$start, $end] = $interval;
|
|
$current = $start;
|
|
while ($current <= $end) {
|
|
if (is_invalid($current)) {
|
|
$invalids[] = $current;
|
|
}
|
|
|
|
$current++;
|
|
}
|
|
}
|
|
|
|
function is_invalid(string $current): bool {
|
|
for ($i = 1, $l = strlen($current); $i < $l; $i++) {
|
|
$parts = str_split($current, $i);
|
|
$is_equals = true;
|
|
for ($j = 0, $m = count($parts) - 1; $j < $m; $j++) {
|
|
if ($parts[$j] !== $parts[$j + 1]) {
|
|
$is_equals = false;
|
|
continue;
|
|
}
|
|
}
|
|
if ($is_equals) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
echo array_sum($invalids); |