diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-09-01 10:42:23 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-09-01 10:42:23 +0100 |
| commit | 593bff55eb416106db7260950715a4ca15732a92 (patch) | |
| tree | a64cc48cb45dcbc91793e053593c20ab8bf79181 | |
| parent | 270524446694cea30010496dde866e902dfdfa5e (diff) | |
| parent | ffa17a2d92f38dd9a431d2cbf96d180fd5a9ce87 (diff) | |
| download | perlweeklychallenge-club-593bff55eb416106db7260950715a4ca15732a92.tar.gz perlweeklychallenge-club-593bff55eb416106db7260950715a4ca15732a92.tar.bz2 perlweeklychallenge-club-593bff55eb416106db7260950715a4ca15732a92.zip | |
Merge pull request #4830 from stuart-little/stuart-little_128_lua
1st commit on 128_lua
| -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) + |
