/
6.php
332 lines (290 loc) · 6.88 KB
/
6.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
<?php
class LightGrid
{
/**
* @var array
*/
protected $lights = [];
protected $defaultState = false;
/** @var LightswitchStrategy */
protected $lightSwitchingStrategy;
/**
*
*/
public function __construct(LightswitchStrategy $lightswitchStrategy)
{
// Construct the grid and turn off all lights by default
for ($x = 0; $x < 1000; $x++) {
for ($y = 0; $y < 1000; $y++) {
$this->lights[$x][$y] = $lightswitchStrategy->getDefaultState();
}
}
$this->lightSwitchingStrategy = $lightswitchStrategy;
}
/**
* @param LightSwitchCommand $command
*/
public function toggleLights(LightSwitchCommand $command)
{
for ($x = $command->getFrom()->getX(); $x <= $command->getTo()->getX(); $x++) {
for ($y = $command->getFrom()->getY(); $y <= $command->getTo()->getY(); $y++) {
$this->lights[$x][$y] = $this->lightSwitchingStrategy->getNewStateForLight(
$x,
$y,
$this->lights[$x][$y],
$command->getCommand()
);
}
}
}
/**
* @return array
*/
public function getLights()
{
return $this->lights;
}
/**
* @param mixed $state
* @return int
*/
public function getNumLights($state)
{
$numLit = 0;
for ($x = 0; $x < 1000; $x++) {
for ($y = 0; $y < 1000; $y++) {
if ($this->lights[$x][$y] === $state) {
$numLit++;
}
}
}
return $numLit;
}
/**
* @return string
*/
public function __toString()
{
$output = '';
for ($y = 0; $y < 1000; $y++) {
for ($x = 0; $x < 1000; $x++) {
$output .= ($this->lights[$x][$y]) ? 1 : 0;
}
$output .= PHP_EOL;
}
return $output;
}
}
interface LightswitchStrategy
{
/**
* @param int $x
* @param int $y
* @param mixed $currentState
* @param string $command
* @return mixed
*/
public function getNewStateForLight($x, $y, $currentState, $command);
/**
* @return mixed
*/
public function getDefaultState();
}
class OnOffLightStrategy implements LightswitchStrategy
{
/**
* @param int $x
* @param int $y
* @param mixed $currentState
* @param string $command
* @return mixed
*/
public function getNewStateForLight($x, $y, $currentState, $command)
{
if ($command == LightSwitchCommand::COMMAND_TOGGLE) {
$newState = !$currentState;
} else {
$newState = ($command == LightSwitchCommand::COMMAND_ON) ? true : false;
}
return $newState;
}
/**
* @return mixed
*/
public function getDefaultState()
{
return false;
}
}
class BrightnessLightStrategy implements LightswitchStrategy
{
/**
* @param int $x
* @param int $y
* @param mixed $currentState
* @param string $command
* @return mixed
*/
public function getNewStateForLight($x, $y, $currentState, $command)
{
switch ($command) {
case LightSwitchCommand::COMMAND_OFF:
if ($currentState > 0) {
$currentState -= 1;
}
break;
case LightSwitchCommand::COMMAND_ON:
$currentState += 1;
break;
case LightSwitchCommand::COMMAND_TOGGLE:
$currentState += 2;
break;
}
return $currentState;
}
/**
* @return mixed
*/
public function getDefaultState()
{
return 0;
}
}
class Coordinate
{
/**
* @var int
*/
private $x;
/**
* @var int
*/
private $y;
/**
* @param int $x
* @param int $y
*/
public function __construct($x, $y)
{
$this->x = (int) $x;
$this->y = (int) $y;
}
/**
* @return int
*/
public function getX()
{
return $this->x;
}
/**
* @return int
*/
public function getY()
{
return $this->y;
}
public static function fromString($string)
{
list($x, $y) = explode(',', $string);
return new self($x, $y);
}
}
class LightSwitchCommand
{
const COMMAND_ON = 1;
const COMMAND_OFF = 2;
const COMMAND_TOGGLE = 3;
/**
* @var Coordinate
*/
protected $from;
/**
* @var Coordinate
*/
protected $to;
/**
* @var string
*/
protected $command;
/**
* @param Coordinate $from
* @param Coordinate $to
* @param string $command
*/
public function __construct(Coordinate $from, Coordinate $to, $command)
{
$this->from = $from;
$this->to = $to;
$this->command = $command;
}
/**
* @return Coordinate
*/
public function getFrom()
{
return $this->from;
}
/**
* @return Coordinate
*/
public function getTo()
{
return $this->to;
}
/**
* @return string
*/
public function getCommand()
{
return $this->command;
}
}
class CommandParser
{
/**
* @param $string
* @return LightSwitchCommand
*/
public function parseCommand($string)
{
if (preg_match('/(toggle|turn on|turn off) (.*) through (.*)/', $string, $matches) == 0)
{
throw new \InvalidArgumentException('Could not parse the instruction "' . $string . '"');
}
switch ($matches[1]) {
case 'toggle':
$command = LightSwitchCommand::COMMAND_TOGGLE;
break;
case 'turn on':
$command = LightSwitchCommand::COMMAND_ON;
break;
case 'turn off':
default:
$command = LightSwitchCommand::COMMAND_OFF;
}
return new LightSwitchCommand(
Coordinate::fromString($matches[2]),
Coordinate::fromString($matches[3]),
$command
);
}
}
$parser = new CommandParser();
$lightGrid = new LightGrid(new OnOffLightStrategy());
foreach (file('6.txt') as $instruction) {
echo 'excecuting ' . $instruction . PHP_EOL;
$lightGrid->toggleLights($parser->parseCommand($instruction));
}
echo 'Part 1 answer: ' . $lightGrid->getNumLights(true);
$lightGrid = new LightGrid(new BrightnessLightStrategy());
foreach (file('6.txt') as $instruction) {
echo 'excecuting ' . $instruction . PHP_EOL;
$lightGrid->toggleLights($parser->parseCommand($instruction));
}
$totalBrightness = 0;
foreach ($lightGrid->getLights() as $x => $arr) {
foreach ($arr as $y => $brightness) {
$totalBrightness += $brightness;
}
}
echo 'Part 2 answer: ' . $totalBrightness;