34 lines
698 B
PHP
34 lines
698 B
PHP
|
<?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;
|
||
|
}
|