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

#
# See https://theweeklychallenge.org/blog/perl-weekly-challenge-003
#

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

{
    row [1] = 1    # 0'th row
    printf "%d\n", row [1]

    for (r = 1; r <= $0; r ++) {
        #
        # Calculate the new row, and print it
        #
        for (i = 1; i <= length (row) + 1; i ++) {
            new [i] = (i == 1                ? 0 : row [i - 1]) +\
                      (i == length (row) + 1 ? 0 : row [i])
            printf ("%s%d", i == 1 ? "" : " ", new [i])
        }
        printf "\n";

        #
        # Copy the new row to the current row
        #
        for (i = 1; i <= length (new); i ++) {
            row [i] = new [i]
        }
    }
}