-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrobot_grid.js
More file actions
18 lines (13 loc) · 744 Bytes
/
robot_grid.js
File metadata and controls
18 lines (13 loc) · 744 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
const robotOnGrid = (grid, path=[[0,0]]) => {
if (grid[0][0]) return [0, 0];
const lastX = path[path.length - 1][0];
const lastY = path[path.length - 1][1];
const right = grid[lastX + 1] ? grid[lastX + 1][lastY] : undefined;
const down = grid[lastX][lastY + 1];
if (right === undefined && down === undefined) return;
if (right) return path.concat([[lastX + 1, lastY]]);
if (down) return path.concat([[lastX, lastY + 1]]);
if (right === undefined) return robotOnGrid(grid, path.concat([[lastX, lastY + 1]]));
if (down === undefined) return robotOnGrid(grid, path.concat([[lastX + 1, lastY]]));
console.log(robotOnGrid(grid, path.concat([[lastX + 1, lastY]])), robotOnGrid(grid, path.concat([[lastX, lastY + 1]])));
};