Finit le jour 9

This commit is contained in:
Clément 2020-12-09 13:06:24 +01:00
parent 1eaf6eece1
commit 88d9f084b4
2 changed files with 47 additions and 0 deletions

22
day_9/part_1.php Normal file
View File

@ -0,0 +1,22 @@
<?php
$numbers = array_map('intval', explode("\n", file_get_contents('input.txt')));
$current_index = 0;
$step = 25;
while (true) {
for ($l = count($numbers) - $step; $current_index <= $l; $current_index++) {
$number_to_test = $numbers[$current_index + $step];
$numbers_to_addition = array_slice($numbers, $current_index, $step);
foreach ($numbers_to_addition as $number) {
if (in_array($number_to_test - $number, $numbers_to_addition, true)) {
$current_index++;
break 2;
}
}
echo $number_to_test;
exit;
}
}

25
day_9/part_2.php Normal file
View File

@ -0,0 +1,25 @@
<?php
$numbers = array_map('intval', explode("\n", file_get_contents('input.txt')));
$current_index = 0;
//$sum = 127;
$sum = 257342611;
while (true) {
$index = $current_index;
$current_sum = 0;
$used_numbers = [];
while ($current_sum <= $sum) {
if ($sum === $current_sum) {
echo min($used_numbers) + max($used_numbers);
exit;
}
$used_numbers[] = $numbers[$index];
$current_sum = array_sum($used_numbers);
$index++;
}
$current_index++;
}