-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCWMazeRunner.js
61 lines (51 loc) · 1.78 KB
/
CWMazeRunner.js
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
//Code Wars: The Maze Runner
//https://www.codewars.com/kata/58663693b359c4a6560001d6
/*Directions
You will be given a 2D array of the maze and an array of directions. Your task is to follow the directions given. If you reach the end point before all your moves have gone, you should return Finish. If you hit any walls or go outside the maze border, you should return Dead. If you find yourself still in the maze after using all the moves you should return Lost.
The maze will look like:
maze = [[1,1,1,1,1,1,1],
[1,0,0,0,0,0,3],
[1,0,1,0,1,0,1],
[0,0,1,0,0,0,1],
[1,0,1,0,1,0,1],
[1,0,0,0,0,0,1],
[1,2,1,0,1,0,1]]
With the following key:
0 = Safe place to walk
1 = Wall
2 = Start Point
3 = Finish Point
Rules
1. The Maze array will always be square i.e. N x N but its size and content will alter from test to test.
2. The start and finish positions will change for the final tests.
3. The directions array will always be in upper case and will be in the format of N = North, E = East, W = West and S = South.
direction = ["N","N","N","N","N","E","E","E","E","E"] == "Finish"
Notes:
I liked this one.
I'd like to revise it to be a bit more streamlined.
*/
function mazeRunner(maze, directions) {
for (var i=0; i < maze.length; i++){
let coord = maze[i].findIndex(n => n===2);
if (coord != -1){
var y=i;
var x=coord;
break;
}
}
for (let direct of directions) {
switch(direct) {
case "N": y--; break;
case "E": x++; break;
case "S": y++; break;
case "W": x--; break;
}
if (!maze[y]) return "Dead";
switch (maze[y][x]){
case undefined:
case 1: return "Dead";
case 3: return "Finish";
}
}
return "Lost";
}