aboutsummaryrefslogtreecommitdiff
path: root/challenge-135/abigail/lua/ch-2.lua
blob: a8c89f57dc1e90a03f0b9d87dd00e67b85926765 (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
#!/opt/local/bin/lua

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

--
-- Run as: lua ch-2.lua < input-file
--

local w = {1, 3, 1, 7, 3, 9, 1}

local valid = "[0-9BCDFGHJKLMNPQRSTVWXYZ]"
local pat   = "^" .. valid .. valid .. valid ..
                     valid .. valid .. valid .. "[0-9]$"

for line in io . lines () do
    if line : find (pat) then
        local check = 0
        for i = 1, 7 do
            local byte = string . byte (line : sub (i, i))
            if   byte <= string . byte ("9")
            then byte  = byte - string . byte ("0")
            else byte  = byte - string . byte ("A")
            end
            check = check + w [i] * byte
         end
         if check % 10 == 0
         then print (1)
         else print (0)
         end
    else
        print (0)
    end
end