-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.js
120 lines (91 loc) · 3.82 KB
/
snake.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
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
/*
We have a two-dimensional board game involving snakes. The board has two types of squares on it: +'s represent impassable squares where snakes cannot go, and 0's represent squares through which snakes can move. Snakes can only enter on the edges of the board, and each snake can move in only one direction. We'd like to find the places where a snake can pass through the entire board, moving in a straight line.
Here is an example board:
col--> 0 1 2 3 4 5 6
+----------------------
row 0 | + + + 0 + 0 0
| 1 | 0 0 + 0 0 0 0
| 2 | 0 0 0 0 + 0 0
v 3 | + + + 0 0 + 0
4 | 0 0 0 0 0 0 0
Write a function that takes a rectangular board with only +'s and 0's, and returns two collections:
* one containing all of the row numbers whose row is completely passable by snakes, and
* the other containing all of the column numbers where the column is completely passable by snakes.
Sample Inputs:
board1 = [['+', '+', '+', '0', '+', '0', '0'],
['0', '0', '+', '0', '0', '0', '0'],
['0', '0', '0', '0', '+', '0', '0'],
['+', '+', '+', '0', '0', '+', '0'],
['0', '0', '0', '0', '0', '0', '0']]
board2 = [['+', '+', '+', '0', '+', '0', '0'],
['0', '0', '0', '0', '0', '+', '0'],
['0', '0', '+', '0', '0', '0', '0'],
['0', '0', '0', '0', '+', '0', '0'],
['+', '+', '+', '0', '0', '0', '+']]
board3 = [['+', '+', '+', '0', '+', '0', '0'],
['0', '0', '0', '0', '0', '0', '0'],
['0', '0', '+', '+', '0', '+', '0'],
['0', '0', '0', '0', '+', '0', '0'],
['+', '+', '+', '0', '0', '0', '+']]
board4 = [['+']]
board5 = [['0']]
All test cases:
findPassableLanes(board1) => Rows: [4], Columns: [3, 6]
findPassableLanes(board2) => Rows: [], Columns: [3]
findPassableLanes(board3) => Rows: [1], Columns: []
findPassableLanes(board4) => Rows: [], Columns: []
findPassableLanes(board5) => Rows: [0], Columns: [0]
Complexity Analysis:
r: number of rows in the board
c: number of columns in the board
*/
const board1 = [['+', '+', '+', '0', '+', '0', '0'],
['0', '0', '+', '0', '0', '0', '0'],
['0', '0', '0', '0', '+', '0', '0'],
['+', '+', '+', '0', '0', '+', '0'],
['0', '0', '0', '0', '0', '0', '0']];
const board2 = [['+', '+', '+', '0', '+', '0', '0'],
['0', '0', '0', '0', '0', '+', '0'],
['0', '0', '+', '0', '0', '0', '0'],
['0', '0', '0', '0', '+', '0', '0'],
['+', '+', '+', '0', '0', '0', '+']];
const board3 = [['+', '+', '+', '0', '+', '0', '0'],
['0', '0', '0', '0', '0', '0', '0'],
['0', '0', '+', '+', '0', '+', '0'],
['0', '0', '0', '0', '+', '0', '0'],
['+', '+', '+', '0', '0', '0', '+']];
const board4 = [['+']];
const board5 = [['0']];
const findPassableLanes = board => {
const rowsMatched = [];
const columnsMatched = [];
let colInRow;
let colsMapping = [];
let colsCheck;
const columnsLength = 7;
const boardLength = board.length;
for (let index = 0; index < boardLength; index++) {
const row = board[index];
const rowMatched = row.every(r=> r === '0');
if (rowMatched) {
rowsMatched.push(index);
}
}
for (let i = 0; i < columnsLength; i++) {
colsMapping = [];
for (let j = 0; j < boardLength; j++) {
colInRow = board[j][i];
colsMapping.push(colInRow);
}
colsCheck = colsMapping.every(r=> r === '0');
if (colsCheck) {
columnsMatched.push(i);
}
}
return {
Rows: rowsMatched,
Columns: columnsMatched,
}
}
console.log(findPassableLanes(board1));
console.log(findPassableLanes(board4));