aboutsummaryrefslogtreecommitdiff
path: root/challenge-126/dave-jacoby/node/ch-2.js
blob: 7157f24e668427b18e8878703f41f39c813c2dcd (plain)
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
49
50
51
52
53
54
55
56
57
58
"use strict";

let minemap = [];
let field = function () {
  /*
x * * * x * x x x x
* * * * * * * * * x
* * * * x * x * x *
* * * x x * * * * *
x * * * x * * * * x
*/
}
  .toString()
  .split(/\/\*/)[1]
  .split(/\*\//)[0]
  .split(/\n/)
  .filter((x) => String(x).match(/\w/));

let maxx = 0;
let maxy = 0;

for (let x in field) {
  maxx = x;
  let rowstr = field[x];
  let row = rowstr.split(/\s+/g);
  minemap[x] = new Array(row.length);
  for (let y in row) {
    maxy = y;
    minemap[x][y] = "0";
    if (row[y] === "x") {
      minemap[x][y] = "x";
    }
  }
}
for (let x = 0; x <= maxx; x++) {
  for (let y = 0; y <= maxy; y++) {
    if (minemap[x][y] === "x") {
      for (let i = -1; i <= 1; i++) {
        for (let j = -1; j <= 1; j++) {
          if (!(i == 0 && i == j)) {
            let xx = x + i;
            let yy = y + j;
            if (xx >= 0 && yy >= 0 && xx <= maxx && yy <= maxy) {
              if (minemap[xx][yy] != "x") {
                minemap[xx][yy]++;
              }
            }
          }
        }
      }
    }
  }
}
for (let x = 0; x <= maxx; x++) {
  console.log(minemap[x].join(" "));
}

// console.log([maxx, maxy, minemap]);