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

#
# See ../README.md
#

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

BEGIN {
    ord_0 = 48
    ord_9 = 57
    ord_A = 65
    ord_Z = 90
    split ("1 3 1 7 3 9 1", w, " ")
    for (i = ord_0; i <= ord_9; i ++) {
        c [sprintf ("%c", i)] = i - ord_0
    }
    for (i = ord_A; i <= ord_Z; i ++) {
        c [sprintf ("%c", i)] = i - ord_A
    }
}


#
# Check for the right characters, and the correct length.
#
/[^0-9BCDFGHJKLMNPQRSTVWXYZ]/ {print 0; next}
length ($0) != 7              {print 0; next}
/[^0-9]$/                     {print 0; next}

#
# Do the check sum
#
{
    check = 0
    for (i = 1; i <= 7; i ++) {
        check += w [i] * c [substr ($0, i, 1)]
    }
    print (check % 10 == 0 ? 1 : 0)
}