-
Notifications
You must be signed in to change notification settings - Fork 0
/
cleanIslands.js
46 lines (38 loc) · 1.27 KB
/
cleanIslands.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
matrix = height = width = newMatrix = counter = counterMax = 0;
// character = 1;
character = String.fromCharCode(9608);
function cleanIslands(data) {
counterMax = 0;
matrix = data;
height = data.length - 1;
width = data[0].length - 1;
const blankLine = Array( width + 1 ).fill(0);
newMatrix = Array( height + 1).fill( null ).map( ()=> Array.from(blankLine) );
matrix.forEach((line, indexY) => {
line.forEach((value, indexX) => {
if ( ( indexY == 0 || indexY == height || indexX == 0 || indexX == width ) && value == 1 ) {
newMatrix[indexY][indexX] = character;
counter = 0;
recursive(indexY, indexX);
counterMax = Math.max(counter, counterMax);
}
});
});
return newMatrix;
}
function recursive(testY, testX) {
counter++;
const sides = [
[0,1], [0,-1], [1,0], [-1,0]
];
sides.forEach(([offsetY, offsetX]) => {
const y = testY - offsetY;
const x = testX - offsetX;
if ( x < 0 || y < 0 || x > width || y > height || matrix[y][x] == 0 || newMatrix[y][x] == character ) {
return;
};
newMatrix[y][x] = character;
recursive(y, x);
});
return;
}