diff options
Diffstat (limited to 'challenge-113/stuart-little/lua')
| -rwxr-xr-x | challenge-113/stuart-little/lua/ch-1.lua | 16 | ||||
| -rwxr-xr-x | challenge-113/stuart-little/lua/ch-2.lua | 49 |
2 files changed, 65 insertions, 0 deletions
diff --git a/challenge-113/stuart-little/lua/ch-1.lua b/challenge-113/stuart-little/lua/ch-1.lua new file mode 100755 index 0000000000..ab387200c5 --- /dev/null +++ b/challenge-113/stuart-little/lua/ch-1.lua @@ -0,0 +1,16 @@ +#!/usr/bin/env lua + +-- run <script> <number> <digit> + +if (arg[1]:find(arg[2]) or tonumber(arg[1]) >= 100 or tonumber(arg[2]) ~= 0 and tonumber(arg[1]) >= 11 * arg[2]) then + print(1) + os.exit() +end +for i = 0,10 do + dff = arg[1]-i*arg[2] + if (dff % 10 == 0 and dff >= 0) then + print(1) + os.exit() + end +end +print(0) diff --git a/challenge-113/stuart-little/lua/ch-2.lua b/challenge-113/stuart-little/lua/ch-2.lua new file mode 100755 index 0000000000..6a4f64e368 --- /dev/null +++ b/challenge-113/stuart-little/lua/ch-2.lua @@ -0,0 +1,49 @@ +#!/usr/bin/env lua + +require 'luarocks.loader' +local inspect = require 'inspect' + +sm=0 +for _,v in ipairs(arg) do + if v ~= "." then sm=sm+v end +end +for k,v in ipairs(arg) do + if v ~= "." then arg[k]=("%d"):format(sm-v) end +end + +function oneOver(t) + local count=0 + for k,v in ipairs(t) do + count = count + (v == "." and 1 or -1) + if count == 1 then return k end + end +end + +function strs2t(lst) + if lst[1] == "." then return end + local val=table.remove(lst,1) + local ix=oneOver(lst) + local llst = {table.unpack(lst,1,ix)} + local rlst = {table.unpack(lst,ix+1)} + return {{val},l=strs2t(llst),r=strs2t(rlst)} +end + +print(inspect(strs2t(arg))) + +--[[ +run <script> <tree in preorder form with '.' for empty nodes, entered as space-separated values> + +ref: https://stackoverflow.com/a/2676849/11064961 + +e.g. 1 2 4 . 7 . . . 3 5 . . 6 . . represents the tree + + 1 + / \ + 2 3 + / / \ + 4 5 6 + \ + 7 + +given as an example in the problem formulation at https://perlweeklychallenge.org/blog/perl-weekly-challenge-113/#TASK2 +--]] |
