aboutsummaryrefslogtreecommitdiff
path: root/challenge-120/abigail/lua/ch-1.lua
blob: 0d5896f59c223f8bf2ad9f25f482a3915a71fb20 (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
#!/opt/local/bin/lua

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

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

for line in io . lines () do
    num = tonumber (line)
    out = 0
    for i = 0, 7 do
        bit = math . floor ((num - (num % (2 ^ i))) / (2 ^ i)) % 2
        if bit == 1
        then
            if i % 2 == 1
            then
                out = out + 2 ^ (i - 1)
            else
                out = out + 2 ^ (i + 1)
            end
        end
    end
    print (out)
end