advent_of_code_2020/day_13/part_1.php

24 lines
531 B
PHP
Raw Permalink Normal View History

2020-12-15 10:18:05 +01:00
<?php
$input = file_get_contents('input.txt');
[$earliest_timestamp, $buses] = explode("\n", $input);
$earliest_timestamp = (int) $earliest_timestamp;
$buses = array_filter(
array_map(static function ($bus) {
return $bus === 'x' ? null : (int) $bus;
}, explode(',', $buses))
);
$timestamp = $earliest_timestamp;
while (true) {
foreach ($buses as $bus) {
if ($timestamp % $bus === 0) {
echo ($timestamp - $earliest_timestamp) * $bus;
exit;
}
}
$timestamp++;
}