diff options
Diffstat (limited to 'challenge-245/roger-bell-west/lua/ch-2.lua')
| -rwxr-xr-x | challenge-245/roger-bell-west/lua/ch-2.lua | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/challenge-245/roger-bell-west/lua/ch-2.lua b/challenge-245/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..063ad63ad8 --- /dev/null +++ b/challenge-245/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,61 @@ +#! /usr/bin/lua + +function combinations(arr, k) + local c = {} + for i = 1, k do + table.insert(c, i) + end + table.insert(c, #arr + 1) + table.insert(c, 0) + local out = {} + while true do + local inner = {} + for i = k, 1, -1 do + table.insert(inner, arr[c[i]]) + end + table.insert(out, inner) + local j = 1 + while c[j] + 1 == c[j + 1] do + c[j] = j + j = j + 1 + end + if j > k then + break + end + c[j] = c[j] + 1 + end + return ipairs(out) +end + +function largestofthree(digits) + ordered = digits + table.sort(ordered, function (i,j) return i < j end) + local mx = 0 + for n = #ordered, 1, -1 do + for _a, c in combinations(ordered, n) do + local t = 0 + for _b, d in ipairs(c) do + t = t * 10 + d + end + if t > mx and t % 3 == 0 then + mx = t + end + end + end + return mx +end + +if largestofthree({8, 1, 9}) == 981 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if largestofthree({8, 6, 7, 1, 0}) == 8760 then + io.write("Pass") +else + io.write("FAIL") +end +print("") + |
