-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
46 lines (42 loc) · 1.1 KB
/
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// score: 160
function revealSabotage(store) {
let prevRow;
let nextRow;
for (const [i, row] of store.entries()) {
nextRow = store[i + 1];
for (const [b, cell] of row.entries()) {
if (cell != '*') {
const calc = +(prevRow?.[b - 1] == '*')
+ +(prevRow?.[b] == '*')
+ +(prevRow?.[b + 1] == '*')
+ +(row?.[b - 1] == '*')
+ +(row?.[b + 1] == '*')
+ +(nextRow?.[b - 1] == '*')
+ +(nextRow?.[b] == '*')
+ +(nextRow?.[b + 1] == '*');
row[b] = calc > 0 ? calc.toString() : ' ';
}
}
prevRow = row;
}
return store;
}
module.exports = revealSabotage;
const store = [
['*', ' ', ' ', ' '],
[' ', ' ', '*', ' '],
[' ', ' ', ' ', ' '],
['*', ' ', ' ', ' ']
];
console.log(revealSabotage(store));
/* Debería mostrar:
[
['*', '2', '1', '1'],
['1', '2', '*', '1'],
['1', '2', '1', '1'],
['*', '1', ' ', ' ']
]
*/
// 00 01 02
// 10 12
// 20 21 22