✨ Finit le jour 2 et commence le 1 et le 3
This commit is contained in:
28
day_2/part_1.php
Normal file
28
day_2/part_1.php
Normal 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);
|
||||
Reference in New Issue
Block a user