-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInstructionHandler.php
193 lines (163 loc) · 4.43 KB
/
InstructionHandler.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
<?php
namespace App\Classes;
use App\Exceptions\InstructionParseException;
/**
* Class InstructionHandler
* @package App\Classes
*/
class InstructionHandler
{
private $output;
private $dataMatrix;
/**
* InstructionHandler constructor.
*/
public function __construct()
{
$this->output = array();
$this->dataMatrix = new DataMatrix();
}
/**
* @return array
*/
public function getOutputAsArray()
{
return $this->output;
}
/**
* @return string
*/
public function getOutputAsString()
{
return implode(PHP_EOL, $this->output);
}
/**
* Process an input string
*
* @param $instructions
*/
public Function processInput($instructions)
{
$lines = $this->getLines($instructions);
$lineNumber = 1;
$numberOfTestCases = $this->getNumberOfTestCases(array_shift($lines), $lineNumber++);
while ($numberOfTestCases > 0) {
$numberOfInstructions =
$this->getSizeAndNumberOfInstructions(array_shift($lines),
$lineNumber++);
while ($numberOfInstructions > 0) {
$this->processInstruction(array_shift($lines),
$lineNumber++);
$numberOfInstructions--;
}
$numberOfTestCases--;
}
}
/**
* Split the input string into a list of lines
*
* @param $input
* @return array
*/
protected function getLines($input)
{
return preg_split("/\r\n|\n|\r/", trim($input));
}
/**
* Get the number of test cases to be executed
*
* @param $line
* @param $lineNumber
* @return int
*/
protected function getNumberOfTestCases($line, $lineNumber)
{
if ($line == null || $line === "") {
throw new InstructionParseException($lineNumber);
}
$numberOfTestCases = 0;
if (sscanf($line, "%d", $numberOfTestCases) !== 1) {
throw new InstructionParseException($lineNumber);
}
return $numberOfTestCases;
}
/**
* Get the size of the matrix and number of instructions to read
*
* @param $line
* @param $lineNumber
* @return int
* @internal param DataMatrix $dataMatrix
*/
protected function getSizeAndNumberOfInstructions($line, $lineNumber)
{
if ($line == null || $line === "") {
throw new InstructionParseException($lineNumber);
}
$size = 0;
$numberOfInstructions = 0;
if (sscanf($line, "%d %d", $size, $numberOfInstructions) !== 2) {
throw new InstructionParseException($lineNumber);
}
$this->dataMatrix->create($size);
return $numberOfInstructions;
}
/**
* Process instruction lines
*
* @param $line
* @param $lineNumber
* @return int|null
* @internal param $output
* @internal param DataMatrix $dataMatrix
*/
protected function processInstruction($line, $lineNumber)
{
if (str_contains($line, 'UPDATE')) {
$this->processUpdate($line, $lineNumber);
} else if (str_contains($line, 'QUERY')) {
$this->output[] = $this->processQuery($line, $lineNumber);
} else {
throw new InstructionParseException($lineNumber);
}
}
/**
* Process update instructions
*
* @param $line
* @param $lineNumber
* @internal param DataMatrix $dataMatrix
*/
protected function processUpdate($line, $lineNumber)
{
$x = 0;
$y = 0;
$z = 0;
$value = 0;
if (sscanf($line, "UPDATE %d %d %d %d", $x, $y, $z, $value) !== 4) {
throw new InstructionParseException($lineNumber);
}
$this->dataMatrix->update($x, $y, $z, $value, $lineNumber);
}
/**
* Process query instructions
*
* @param $line
* @param $lineNumber
* @return int
* @internal param DataMatrix $dataMatrix
*/
protected function processQuery($line, $lineNumber)
{
$x1 = 0;
$y1 = 0;
$z1 = 0;
$x2 = 0;
$y2 = 0;
$z2 = 0;
if (sscanf($line, "QUERY %d %d %d %d %d %d", $x1, $y1, $z1, $x2, $y2, $z2) !== 6) {
throw new InstructionParseException($lineNumber);
}
return $this->dataMatrix->query($x1, $y1, $z1, $x2, $y2, $z2, $lineNumber);
}
}