-
Notifications
You must be signed in to change notification settings - Fork 0
/
maze.php
205 lines (173 loc) · 5.24 KB
/
maze.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
<?php
/**
* Ethan Wright
* Date: 7/20/2015
* Laser maze coding challenge
*/
Class Maze
{
private $width;
private $height;
private $start_x;
private $start_y;
private $start_direction;
private $mirrors;
function Maze($x, $y)
{
$this->width = $x;
$this->height = $y;
# Create mirror arrays
$this->forward_mirrors = array();
$this->backward_mirrors = array();
# Null out the mirror array
for ($mirror_x = 0; $mirror_x <= $x; $mirror_x++) {
$this->mirrors[$mirror_x] = array();
for ($mirror_y = 0; $mirror_y < $y; $mirror_y++) {
$this->mirrors[$mirror_x][$mirror_y] = '';
}
}
}
function forwardTransform($direction) {
# Forward slash is swap N<->E and S<->W
if ($direction == 'N') {
return 'E';
} elseif ($direction == 'E') {
return 'N';
} elseif ($direction == 'S') {
return 'W';
} elseif ($direction == 'W') {
return 'S';
}
return '';
}
function backwardTransform($direction) {
# Back slash is swap N<->W and S<->E
if ($direction == 'N') {
return 'W';
} elseif ($direction == 'E') {
return 'S';
} elseif ($direction == 'S') {
return 'E';
} elseif ($direction == 'W') {
return 'N';
}
return '';
}
function hasBackwardMirror($x, $y)
{
if ($this->mirrors[$x][$y] == '\\') {
return true;
} else {
return false;
}
}
function hasForwardMirror($x, $y)
{
if ($this->mirrors[$x][$y] == '/') {
return true;
} else {
return false;
}
}
public function setStart($x, $y, $direction)
{
$this->start_x = $x;
$this->start_y = $y;
$this->start_direction = $direction;
}
public function getStart()
{
return array($this->start_x, $this->start_y, $this->start_direction);
}
public function setMirror($x, $y, $type)
{
$this->mirrors[$x][$y] = $type;
}
public function shootLaser()
{
$direction = $this->start_direction;
$x = $this->start_x;
$y = $this->start_y;
$count = 0;
# Continue until an edge is crossed
while (($x >= 0 && $x < $this->width) && ($y >= 0 && $y < $this->height)) {
# Is there a mirror at these coordinates?
if ($this->hasForwardMirror($x, $y)) {
# Swap N/E and S/W
$direction = $this->forwardTransform($direction);
}
if ($this->hasBackwardMirror($x, $y)) {
# Swap N/W and S/E
$direction = $this->backwardTransform($direction);
}
# Move the position
if ($direction == 'N') {
$y += 1;
} elseif ($direction == 'E') {
$x += 1;
} elseif ($direction == 'S') {
$y -= 1;
} elseif ($direction == 'W') {
$x -= 1;
}
# Are we stuck in a loop?
# We will know if we are back on the starting coordinates heading in the starting direction
if ($x == $this->start_x && $y == $this->start_y && $direction == $this->start_direction) {
return array($count, -1, -1);
}
$count += 1;
}
# We went one square too far in order to find the grid exit, so go back one square
$count -= 1;
$x = $x < 0 ? 0 : $x;
$x = $x >= $this->width ? $this->width - 1 : $x;
$y = $y < 0 ? 0 : $y;
$y = $y >= $this->height ? $this->height - 1 : $y;
return array($count, $x, $y);
}
};
########################
# Code entry block #
########################
if (!count(debug_backtrace())) {
if (count($argv) > 2) {
$input_file = $argv[1];
$output_file = $argv[2];
} else {
# No input file or output file provided
print "Usage: php maze ./path/to/input/file ./path/to/output/file\n";
exit();
}
$input = fopen($input_file, 'r');
while ($line = fgets($input)) {
$coordinates = explode(' ', trim($line));
$x = $coordinates[0];
$y = $coordinates[1];
# Grid dimensions
if (count($coordinates) == 2) {
$maze = new Maze($x, $y);
# Player coordinates
} elseif ($coordinates[2] == 'S') {
$maze->setStart($x, $y, $coordinates[2]);
# Mirror
} elseif ($coordinates[2] == '/' || $coordinates[2] == '\\') {
$maze->setMirror($x, $y, $coordinates[2]);
# Invalid
} else {
print "Usage: php maze ./path/to/input/file ./path/to/output/file\n";
fclose($input);
exit();
}
}
# SHOOT!
$result = $maze->shootLaser();
# Check if they got stuck in a loop and write to output file
$output = fopen($output_file, 'w');
if ($result[1] == -1) {
fwrite($output, "{$result[0]}\r\nStuck in a loop");
} else {
fwrite($output, "{$result[0]}\r\n{$result[1]} {$result[2]}");
}
fclose($output);
exit();
}