@ParasArora/

cavity map

Nodejs

No description

fork
loading
Files
  • index.js
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
function cavityMap(grid) {
  let directions = [
    [-1, 0], // up
    [ 1, 0], // down
    [ 0,-1], // left
    [ 0, 1] // right
  ]

  for(let i = 1; i<grid.length-1; i++){
    for(let j = 1; j < grid.length - 1; j++){
      let gridPos = grid[i][j];
      let count = 0;
      for(let dir of directions){
        
        let pos = grid[i + dir[0]][j + dir[1]];
        if(pos < gridPos && pos !== 'X'){
          count++
        }
        

      }

      if(count == 4) {
        grid[i] = grid[i].substr(0, j) + 'X' + grid[i].substr(j + 1);
      }

    }

  }

  return grid;
}

cavityMap([
  "1112",
  "1912",
  "1892",
  "1234"
]);
// cavityMap([
//   "6254",
//   "9979",
//   "9153",
//   "6678"
// ]);
node v10.16.0