aboutsummaryrefslogtreecommitdiff
path: root/challenge-251/roger-bell-west/lua/ch-1.lua
blob: e893bf6fbd442a86c348ccc4708c8e94020ee767 (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
#! /usr/bin/lua

function concat(a0, b0)
   if b0 == 0 then
      return 10 * a0
   end
   local a = a0
   local b = b0
   while b > 0 do
      a = a * 10
      b = math.floor(b / 10)
   end
   return a + b0
end

function concatenationvalue(a)
   local t = 0
   for i = 1, math.floor((#a + 1) / 2) do
      local j = #a + 1 - i
      if j == i then
         t = t + a[i]
      else
         t = t + concat(a[i], a[j])
      end
   end
   return t
end

if concatenationvalue({6, 12, 25, 1}) == 1286 then
  io.write("Pass")
else
  io.write("FAIL")
end
io.write(" ")

if concatenationvalue({10, 7, 31, 5, 2, 2}) == 489 then
  io.write("Pass")
else
  io.write("FAIL")
end
io.write(" ")

if concatenationvalue({1, 2, 10}) == 112 then
  io.write("Pass")
else
  io.write("FAIL")
end
print("")