-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
26 lines (23 loc) · 822 Bytes
/
index.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
/* eslint-disable function-paren-newline */
/* eslint-disable operator-linebreak */
/* eslint-disable implicit-arrow-linebreak */
function detectBombs(grid) {
const neighborOffsets = [-1, 0, 1];
return grid.map((row, rowIndex) =>
row.map((_, colIndex) => {
const adjacentBombsCount = neighborOffsets.reduce(
(totalBombs, rowOffset) =>
totalBombs +
neighborOffsets.reduce((bombsInColumn, colOffset) => {
const neighborRow = rowIndex + rowOffset;
const neighborCol = colIndex + colOffset;
const isBomb = grid[neighborRow]?.[neighborCol];
return bombsInColumn + (isBomb ? 1 : 0);
}, 0),
0,
);
return adjacentBombsCount - (grid[rowIndex][colIndex] ? 1 : 0);
}),
);
}
module.exports = detectBombs;