diff options
| -rwxr-xr-x | challenge-128/stuart-little/lua/ch-1.lua | 46 | ||||
| -rwxr-xr-x | challenge-128/stuart-little/lua/ch-2.lua | 27 |
2 files changed, 73 insertions, 0 deletions
diff --git a/challenge-128/stuart-little/lua/ch-1.lua b/challenge-128/stuart-little/lua/ch-1.lua new file mode 100755 index 0000000000..8f3a6fe354 --- /dev/null +++ b/challenge-128/stuart-little/lua/ch-1.lua @@ -0,0 +1,46 @@ +#!/usr/bin/env lua + +-- run <script> <space-separated binary words, with one word representing each row> + +function mrg(s1,s2) + local out = "" + for i=1,s1:len() do + out = out .. tostring(math.max(tonumber(s1:sub(i,i)), tonumber(s2:sub(i,i)))) + end + return out +end + +function reduce(fn,tab) + local out = tab[1] + for i=2,#tab do + out = fn(out,tab[i]) + end + return out +end + +function maxZeros(s) + local mx=0 + for seg in string.gmatch(s,"0+") do + if seg:len() > mx then mx = seg:len() end + end + return mx +end + +local rows=0 +local cols=0 + +for i=1,#arg do + for j=i,#arg do + --print(type(reduce(mrg,table.pack(table.unpack(arg,i,j))))) + local cl = maxZeros(reduce(mrg,table.pack(table.unpack(arg,i,j)))) + if (j-i+1)*cl > rows*cols then + rows, cols = j-i+1, cl + end + end +end + +for _=1,rows do + local row="" + for _=1,cols do row = row .. '0' end + print(row) +end diff --git a/challenge-128/stuart-little/lua/ch-2.lua b/challenge-128/stuart-little/lua/ch-2.lua new file mode 100755 index 0000000000..f58bc3bda0 --- /dev/null +++ b/challenge-128/stuart-little/lua/ch-2.lua @@ -0,0 +1,27 @@ +#!/usr/bin/env lua + +-- run <script> <starting arrivals followed by departures, all space-separated> + +local times={} +for k,v in ipairs(arg) do + table.insert(times,{v,1 + ((k-1) % math.floor(#arg/2))}) +end +table.sort(times, function(a,b) if a[1] < b[1] then return true else return false end end) + +local sol=0 +local station={} +for i=1,math.floor(#arg/2) do + table.insert(station,0) +end + +for _,v in ipairs(times) do + station[v[2]] = station[v[2]] ~ 1 + local sm=0 + for _,val in ipairs(station) do + sm = sm + val + end + if sm > sol then sol = sm end +end + +print(sol) + |
