diff options
Diffstat (limited to 'challenge-092')
| -rwxr-xr-x | challenge-092/stuart-little/lua/ch-1.lua | 14 | ||||
| -rwxr-xr-x | challenge-092/stuart-little/lua/ch-2.lua | 36 |
2 files changed, 50 insertions, 0 deletions
diff --git a/challenge-092/stuart-little/lua/ch-1.lua b/challenge-092/stuart-little/lua/ch-1.lua new file mode 100755 index 0000000000..4e7c0e1a28 --- /dev/null +++ b/challenge-092/stuart-little/lua/ch-1.lua @@ -0,0 +1,14 @@ +#!/usr/bin/env lua + +-- run <script> <space-separated strings> + +function oneWay(s1,s2) + local t={} + for i=1,s1:len() do + if i>s2:len() then return false end + if not t[s1:sub(i,i)] then t[s1:sub(i,i)] = s2:sub(i,i) end + end + return s2 == s1:gsub(".",t) +end + +print(oneWay(arg[1],arg[2]) and oneWay(arg[2],arg[1]) and 1 or 0) diff --git a/challenge-092/stuart-little/lua/ch-2.lua b/challenge-092/stuart-little/lua/ch-2.lua new file mode 100755 index 0000000000..4f100e87ef --- /dev/null +++ b/challenge-092/stuart-little/lua/ch-2.lua @@ -0,0 +1,36 @@ +#!/usr/bin/env lua + +--[[ +run <script> <initial ordered intervals followed by extra interval> + +each interval should be entered as <left> <space> <right>, with spaces between the individual intervals + +e.g. <script> 1 4 8 10 2 6 +--]] + +function lt(t1,t2) + return t1 and t2 and t1[2] < t2[1] +end + +function overlap(t1,t2) + return t1 and t2 and t1[2] >= t2[1] and t2[2] >= t1[1] +end + +function mergeOver(t1,t2) + return {math.min(t1[1],t2[1]),math.max(t1[2],t2[2])} +end + +local ints={} +while #arg>0 do + table.insert(ints,{tonumber(table.remove(arg,1)),tonumber(table.remove(arg,1))}) +end +extra=table.remove(ints) + +local i=1 +while lt(ints[i],extra) do i=i+1 end +while overlap(ints[i],extra) do + local t=table.remove(ints,i) + extra=mergeOver(extra,t) +end +table.insert(ints,i,extra) +for _,t in ipairs(ints) do print(table.unpack(t)) end |
