Browse Source

Finit le 15e jour

main
Clément 10 months ago
parent
commit
da17833029
  1. 33
      day_15/part_1.php
  2. 39
      day_15/part_2.php

33
day_15/part_1.php

@ -0,0 +1,33 @@
<?php
$starting_numbers = explode(',', file_get_contents('input.txt'));
$numbers = array_map('intval', $starting_numbers);
$number = array_pop($numbers);
for ($i = count($starting_numbers); $i <= 2020; $i++) {
$number = newNumber($number);
}
echo $number;
function newNumber(int $number = 0)
{
global $numbers;
if (!in_array($number, $numbers, true)) {
$numbers[] = $number;
return $number;
}
$indexes = array_keys($numbers, $number);
if (count($indexes) === 1) {
$numbers[] = 0;
return 0;
}
$new_number = $indexes[count($indexes) - 1] - $indexes[count($indexes) - 2];
$numbers[] = $new_number;
return $new_number;
}

39
day_15/part_2.php

@ -0,0 +1,39 @@
<?php
$starting_numbers = explode(',', file_get_contents('input.txt'));
$tmp_numbers = array_map('intval', $starting_numbers);
$count_numbers = [];
foreach ($tmp_numbers as $i => $tmp_number) {
if (array_key_exists($tmp_number, $count_numbers)) {
$count_numbers[$tmp_number][] = $i;
} else {
$count_numbers[$tmp_number] = [$i];
}
}
$number = end($tmp_numbers);
unset($tmp_numbers, $tmp_number);
for ($i = count($starting_numbers); $i <= 29999999; $i++) {
$number = newNumber($i, $number);
}
echo $number;
function newNumber(int $i, int $number = 0)
{
global $count_numbers;
if (!array_key_exists($number, $count_numbers)) {
$count_numbers[$number] = [$i];
return $number;
}
$count_indexes = count($count_numbers[$number]);
$new_number = $count_indexes === 1 ? 0 : $count_numbers[$number][$count_indexes - 1] - $count_numbers[$number][$count_indexes - 2];
$count_numbers[$new_number][] = $i;
return $new_number;
}
Loading…
Cancel
Save