aboutsummaryrefslogtreecommitdiff
path: root/challenge-126/abigail/node/ch-2.js
blob: 508e6b70f1efbddb59dc5f84135c39d0af76af51 (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
#!/usr/local/bin/node

//
// See ../README.md
//

//
// Run as: node ch-2.js < input-file
//

let board = []

  require ('readline')
. createInterface ({input: process . stdin})   
. on              ('line', _ => {board . push (_ . trim () . split (" "))})
. on              ('close',  () => {
    let X = board . length
    let Y = board [0] . length
    for (x = 0; x < X; x ++) {
        for (y = 0; y < Y; y ++) {
            if (y != 0) {
                process . stdout . write (" ")
            }
            if (board [x] [y] == "x") {
                process . stdout . write ("x")
                continue
            }
            let count = 0
            for (dx = -1; dx <= 1; dx ++) {
                if (x + dx < 0 || x + dx >= X) {
                    continue
                }
                for (dy = -1; dy <= 1; dy ++) {
                    if (x + dx < 0 || x + dx >= X) {
                        continue
                    }
                    if (dx == 0 && dy == 0) {
                        continue
                    }
                    if (board [x + dx] [y + dy] == "x") {
                        count ++
                    }
                }
            }
            process . stdout . write (count . toString ())
        }
        process . stdout . write ("\n")
    }
})