aboutsummaryrefslogtreecommitdiff
path: root/challenge-126/abigail/awk/ch-2.awk
blob: c9287328293758cd986ab5d657623162f565cbb7 (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
#!/usr/bin/awk

#
# See ../README.md
#

#
# Run as: awk -f ch-2.awk < input-file
#

{
    for (i = 1; i <= NF; i ++) {
        board [NR, i] = $i
    }
    X = NR
    Y = NF
}

END {
    for (x = 1; x <= X; x ++) {
        for (y = 1; y <= Y; y ++) {
            if (board [x, y] == "x") {
                if (y > 1) {
                    printf (" ")
                }
                printf (board [x, y])
                continue
            }
            count = 0
            for (dx = -1; dx <= 1; dx ++) {
                if (x + dx < 1 || x + dx > X) {
                    continue
                }
                for (dy = -1; dy <= 1; dy ++) {
                    if (y + dy < 1 || y + dy > Y ||
                        (dx == 0 && dy == 0)) {
                        continue
                    }
                    if (board [x + dx, y + dy] == "x") {
                        count ++
                    }
                }
            }
            if (y > 1) {
                printf (" ")
            }
            printf (count)
        }
        printf ("\n")
    }
}