Finit le jour 2 et commence le 1 et le 3

This commit is contained in:
Clement Desmidt
2025-12-10 15:01:52 +01:00
commit 8b73ada230
6 changed files with 139 additions and 0 deletions

28
day_2/part_1.php Normal file
View File

@@ -0,0 +1,28 @@
<?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);