-
Notifications
You must be signed in to change notification settings - Fork 1
/
solution.js
48 lines (36 loc) · 937 Bytes
/
solution.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
/**
* @param {boolean[][]} grid
* @returns {number[][]}
*/
export default function detectBombs(grid) {
/** @type {number[][]}>} */
const adjacentsBombs = []
for (let i = 0; i < grid.length; i++) {
/** @type {boolean[]}>} */
const row = grid[i]
/** @type {number[]}>} */
const bombsInRow = []
for (let j = 0; j < row.length; j++) {
/** @type {number}>} */
const startI = i - 1
/** @type {number}>} */
const stopI = i + 2
/** @type {number}>} */
const startJ = j - 1
/** @type {number}>} */
const stopJ = j + 2
/** @type {number}>} */
let bombs = 0
for (let _i = startI; _i < stopI; _i++) {
if (_i < 0 || _i > grid.length - 1) continue
for (let _j = startJ; _j < stopJ; _j++) {
if (_j < 0 || _j > row.length - 1) continue
bombs += ~~grid[_i][_j]
}
}
bombsInRow.push(bombs - ~~row[j])
}
adjacentsBombs.push(bombsInRow)
}
return adjacentsBombs
}