|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace adventofcode\Year2015; |
| 4 | + |
| 5 | +class ReindeerOlympics |
| 6 | +{ |
| 7 | + /** |
| 8 | + * Determine the maximum distance flown from a given set of reindeer. |
| 9 | + * |
| 10 | + * @param array $reindeerData |
| 11 | + * @param int $interval |
| 12 | + * @return float |
| 13 | + */ |
| 14 | + public function determineFlightWinnerDistance(array $reindeerData, int $interval): float |
| 15 | + { |
| 16 | + $winnersDistance = 0; |
| 17 | + |
| 18 | + foreach ($reindeerData as $reindeer) { |
| 19 | + $data = $this->parseReindeerData($reindeer); |
| 20 | + $distance = $this->calculateDistance( |
| 21 | + $data['speed'], |
| 22 | + $data['flightTime'], |
| 23 | + $data['oneSecondFlightDistance'], |
| 24 | + $data['restDuration'], |
| 25 | + $interval |
| 26 | + ); |
| 27 | + if ($distance > $winnersDistance) { |
| 28 | + $winnersDistance = $distance; |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + return $winnersDistance; |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Determine the winning points from a given set of reindeer. |
| 37 | + * |
| 38 | + * @param array $reindeerData |
| 39 | + * @param int $interval |
| 40 | + * @return int |
| 41 | + */ |
| 42 | + public function determineFlightWinnerPoints(array $reindeerData, int $interval): int |
| 43 | + { |
| 44 | + $contestants = []; |
| 45 | + |
| 46 | + foreach ($reindeerData as $reindeer) { |
| 47 | + $data = $this->parseReindeerData($reindeer); |
| 48 | + $contestants[] = $data; |
| 49 | + } |
| 50 | + |
| 51 | + for ($i = 0; $i < $interval; $i++) { |
| 52 | + // Determine each reindeer's current distance. |
| 53 | + foreach ($contestants as $index => $contestant) { |
| 54 | + if (!$contestant['isResting']) { |
| 55 | + $contestants[$index]['currentDistance'] += $contestant['speed']; |
| 56 | + |
| 57 | + if ($contestants[$index]['currentLeg'] + 1 === $contestant['flightTime']) { |
| 58 | + $contestants[$index]['isResting'] = true; |
| 59 | + $contestants[$index]['currentLeg'] = 0; |
| 60 | + |
| 61 | + continue; |
| 62 | + } |
| 63 | + |
| 64 | + $contestants[$index]['currentLeg']++; |
| 65 | + } |
| 66 | + |
| 67 | + if ($contestant['isResting'] && $contestant['currentRestTime'] + 1 === $contestant['restDuration']) { |
| 68 | + $contestants[$index]['isResting'] = false; |
| 69 | + $contestants[$index]['currentRestTime'] = 0; |
| 70 | + |
| 71 | + continue; |
| 72 | + } |
| 73 | + |
| 74 | + if ($contestant['isResting']) { |
| 75 | + $contestants[$index]['currentRestTime']++; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + // Find the largest current distance |
| 80 | + $largestDistance = 0; |
| 81 | + |
| 82 | + foreach ($contestants as $contestant) { |
| 83 | + if ($contestant['currentDistance'] > $largestDistance) { |
| 84 | + $largestDistance = $contestant['currentDistance']; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + // Update the scores for those in the lead. |
| 89 | + foreach ($contestants as $index => $contestant) { |
| 90 | + if ($contestant['currentDistance'] === $largestDistance) { |
| 91 | + $contestants[$index]['currentPoints']++; |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + $winnersPoints = 0; |
| 97 | + |
| 98 | + foreach ($contestants as $contestant) { |
| 99 | + if ($contestant['currentPoints'] > $winnersPoints) { |
| 100 | + $winnersPoints = $contestant['currentPoints']; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + return $winnersPoints; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Find the distance covered over a given interval based on the given speed, flight time, and rest duration. |
| 109 | + * All values are in seconds. |
| 110 | + * |
| 111 | + * @param int $speed |
| 112 | + * @param int $flightTime |
| 113 | + * @param float $oneSecondFlightDistance |
| 114 | + * @param int $restDuration |
| 115 | + * @param int $interval |
| 116 | + * @return float |
| 117 | + */ |
| 118 | + protected function calculateDistance( |
| 119 | + int $speed, |
| 120 | + int $flightTime, |
| 121 | + float $oneSecondFlightDistance, |
| 122 | + int $restDuration, |
| 123 | + int $interval |
| 124 | + ): float { |
| 125 | + $distance = 0; |
| 126 | + |
| 127 | + $maxFlightTime = $speed * $flightTime; |
| 128 | + |
| 129 | + $currentTime = 0; |
| 130 | + while ($currentTime < $interval) { |
| 131 | + if ($maxFlightTime > $interval) { |
| 132 | + $distance += $oneSecondFlightDistance * ($interval - $currentTime); |
| 133 | + $currentTime = $interval; |
| 134 | + continue; |
| 135 | + } |
| 136 | + |
| 137 | + if ($maxFlightTime + $restDuration > $interval) { |
| 138 | + $distance += $maxFlightTime + ($interval - $currentTime); |
| 139 | + $currentTime = $interval; |
| 140 | + continue; |
| 141 | + } |
| 142 | + |
| 143 | + $distance += $maxFlightTime; |
| 144 | + $currentTime += $flightTime + $restDuration; |
| 145 | + } |
| 146 | + |
| 147 | + return $distance; |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Parse a string of reindeer flight data. |
| 152 | + * |
| 153 | + * @param string $reindeer |
| 154 | + * @return array |
| 155 | + */ |
| 156 | + protected function parseReindeerData(string $reindeer): array |
| 157 | + { |
| 158 | + preg_match('/(.*) can fly (\d+) km\/s for (\d+).*for (\d+).*/', $reindeer, $matches); |
| 159 | + |
| 160 | + return [ |
| 161 | + 'name' => $matches[1], |
| 162 | + 'speed' => intval($matches[2]), |
| 163 | + 'flightTime' => intval($matches[3]), |
| 164 | + 'oneSecondFlightDistance' => $matches[2] / $matches[3], |
| 165 | + 'maxFlightTime' => $matches[2] * $matches[3], |
| 166 | + 'restDuration' => intval($matches[4]), |
| 167 | + 'currentDistance' => 0, |
| 168 | + 'currentPoints' => 0, |
| 169 | + 'currentLeg' => 0, |
| 170 | + 'isResting' => false, |
| 171 | + 'currentRestTime' => 0, |
| 172 | + ]; |
| 173 | + } |
| 174 | +} |
0 commit comments