$deers = set();
foreach ($input->lines as $line) {
preg_match("/^(\w+) can fly (\d+) km\/s for (\d+) seconds, but then must rest for (\d+) seconds\.$/", $line, $matches);
$deers[$matches[1]] = array_slice($matches, 2);
}
function get_distance($deer, $time)
{
$cycles = floor($time / ($deer[1] + $deer[2])) * ($deer[1] * $deer[0]);
$left = $time % ($deer[1] + $deer[2]);
return $cycles + min($left, $deer[1]) * $deer[0];
}
// ==================================================
// > PART 1
// ==================================================
$solution_1 = $deers->map(fn ($deer) => get_distance($deer, 2503))->max();
// ==================================================
// > PART 2
// ==================================================
$points = [];
for ($i = 1; $i <= 2503; $i++) {
$distances = $deers->mapAssoc(fn ($name, $deer) => [$name => get_distance($deer, $i)]);
foreach ($distances->keep($distances->max())->keys() as $name) {
$points[$name] = ($points[$name] ?? 0) + 1;
}
}
$solution_2 = max($points);