-
Notifications
You must be signed in to change notification settings - Fork 0
/
14.php
200 lines (167 loc) · 4.69 KB
/
14.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
$lines = [
'Comet can fly 14 km/s for 10 seconds, but then must rest for 127 seconds.',
'Dancer can fly 16 km/s for 11 seconds, but then must rest for 162 seconds.'
];
$lines = file('14.txt');
$race = new Race();
// Add reindeers to race
foreach ($lines as $line) {
preg_match('/(.*) can fly ([0-9]+) km\/s for ([0-9]+) seconds, but then must rest for ([0-9]+) seconds\./', $line, $matches);
$race->addReindeer(new Reindeer($matches[1], $matches[2], $matches[3], $matches[4]));
}
$race->startRace(2503);
$winner = $race->getLeaders()[0];
echo array_sum($race->getPoints());
foreach ($race->getReindeers() as $reindeer) {
printf("%s: %d km, %d points\n", $reindeer->getName(), $reindeer->getDistanceCovered(), $race->getPointsForReindeer($reindeer));
}
echo sprintf('Part 1 answer: %s', $winner->getDistanceCovered()) . PHP_EOL;
echo sprintf('Part 2 answer: %s', max($race->getPoints())) . PHP_EOL;
class Race
{
/** @var Reindeer[] */
protected $reindeers = [];
/** @var array */
protected $points = [];
/**
* @param Reindeer $reindeer
*/
public function addReindeer(Reindeer $reindeer)
{
$this->reindeers[] = $reindeer;
}
/**
* @return Reindeer[]
*/
public function getReindeers()
{
return $this->reindeers;
}
/**
* @param int $duration
*/
public function startRace($duration)
{
$this->points = [];
for ($i = 1; $i <= $duration; $i++) {
foreach ($this->reindeers as $reindeer) {
$reindeer->advance();
}
$this->awardPointToLeader();
}
}
/**
* Award a point to the reindeer currently leading
*/
protected function awardPointToLeader()
{
foreach ($this->getLeaders() as $leader) {
if (!isset($this->points[$leader->getName()])) {
$this->points[$leader->getName()] = 0;
}
$this->points[$leader->getName()]++;
}
}
/**
* Get reindeer which has covered the most distance
*
* @return Reindeer[]
*/
public function getLeaders()
{
usort($this->reindeers, function(Reindeer $a, Reindeer $b) {
if ($a->getDistanceCovered() == $b->getDistanceCovered()) {
return 0;
}
return ($a->getDistanceCovered() < $b->getDistanceCovered()) ? 1 : -1;
});
$leaderDistance = $this->reindeers[0]->getDistanceCovered();
$leaders = [];
foreach ($this->reindeers as $reindeer) {
if ($reindeer->getDistanceCovered() !== $leaderDistance) {
break;
}
$leaders[] = $reindeer;
}
return $leaders;
}
/**
* @param Reindeer $reindeer
* @return int
*/
public function getPointsForReindeer(Reindeer $reindeer)
{
if (!isset($this->points[$reindeer->getName()])) {
return 0;
}
return $this->points[$reindeer->getName()];
}
/**
* @return array
*/
public function getPoints()
{
return $this->points;
}
}
class Reindeer
{
const STATE_RESTING = 1;
const STATE_FLYING = 2;
/** @var string */
protected $currentState = self::STATE_FLYING;
/** @var string */
protected $name;
/** @var int */
protected $flySpeed;
/** @var int */
protected $flyDuration;
/** @var int */
protected $restDuration;
/** @var int */
protected $distanceCovered = 0;
/** @var int */
protected $counter = 0;
/**
* @param string $name
* @param int $flySpeed
* @param int $flyDuration
* @param int $restDuration
*/
public function __construct($name, $flySpeed, $flyDuration, $restDuration)
{
$this->name = $name;
$this->flySpeed = (int) $flySpeed;
$this->flyDuration = (int) $flyDuration;
$this->restDuration = (int) $restDuration;
$this->counter = (int) $flyDuration;
}
/**
* Advance the Reindeer
*/
public function advance()
{
if ($this->currentState == self::STATE_FLYING) {
$this->distanceCovered += $this->flySpeed;
}
if (--$this->counter === 0) {
$this->currentState = ($this->currentState == self::STATE_FLYING) ? self::STATE_RESTING : self::STATE_FLYING;
$this->counter = $this->currentState == self::STATE_FLYING ? $this->flyDuration : $this->restDuration;
}
}
/**
* @return int
*/
public function getDistanceCovered()
{
return $this->distanceCovered;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
}