diff options
| author | chirvasitua <stuart-little@users.noreply.github.com> | 2021-08-31 18:00:26 -0400 |
|---|---|---|
| committer | chirvasitua <stuart-little@users.noreply.github.com> | 2021-08-31 18:00:26 -0400 |
| commit | ffa17a2d92f38dd9a431d2cbf96d180fd5a9ce87 (patch) | |
| tree | c8981429e67b12d9561598cc26ec1bb3d5f2bc9b | |
| parent | 7579d184412cc66d8a2a4ad096e5c8dda0a477db (diff) | |
| download | perlweeklychallenge-club-ffa17a2d92f38dd9a431d2cbf96d180fd5a9ce87.tar.gz perlweeklychallenge-club-ffa17a2d92f38dd9a431d2cbf96d180fd5a9ce87.tar.bz2 perlweeklychallenge-club-ffa17a2d92f38dd9a431d2cbf96d180fd5a9ce87.zip | |
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) + |
