Ejemplo n.º 1
0
 public function createReindeer($line)
 {
     list($name, , , $flySpeed, , , $flightDuration, , , , , , , $restTime) = explode(" ", $line);
     $reindeer = new Reindeer();
     $reindeer->setName($name)->setFlySpeed($flySpeed)->setFlightDuration($flightDuration)->setRestTime($restTime);
     return $reindeer;
 }
Ejemplo n.º 2
0
 /**
  * @param Reindeer $reindeer
  * @return int
  */
 public function getPointsForReindeer(Reindeer $reindeer)
 {
     if (!isset($this->points[$reindeer->getName()])) {
         return 0;
     }
     return $this->points[$reindeer->getName()];
 }
Ejemplo n.º 3
0
<?php

$reindeerStats = file('../inputs/Day14.txt');
if (empty($reindeerStats) || !is_array($reindeerStats)) {
    exit("Unable to load input file");
}
$allReindeer = array();
foreach ($reindeerStats as $line) {
    preg_match('/^(?<name>[A-Z][a-z]+) .+? (?<speed>[\\d]+) .+? (?<flight>[\\d]+) .+? (?<rest>[\\d]+) .+$/', $line, $matches);
    $reindeer = new Reindeer($matches['name'], $matches['speed'], $matches['flight'], $matches['rest']);
    $allReindeer[] = $reindeer;
}
$totalTime = 2503;
$maxDistance = 0;
for ($s = 0; $s < $totalTime; $s++) {
    foreach ($allReindeer as $reindeer) {
        $reindeer->tick();
        $maxDistance = max($maxDistance, $reindeer->distance);
    }
    foreach ($allReindeer as $reindeer) {
        if ($reindeer->distance == $maxDistance) {
            $reindeer->score++;
        }
    }
}
$highestScore = 0;
foreach ($allReindeer as $reindeer) {
    $highestScore = max($highestScore, $reindeer->score);
}
print "\nPart 1: {$maxDistance}\nPart 2: {$highestScore}\n";
class Reindeer