28 lines
614 B
PHP
28 lines
614 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($current): bool {
|
|
if (strlen($current) % 2 !== 0) {
|
|
return false;
|
|
}
|
|
|
|
$middle = strlen($current) / 2;
|
|
return substr($current, 0, $middle) === substr($current, $middle);
|
|
}
|
|
|
|
echo array_sum($invalids); |