diff options
| author | chirvasitua <chirvasitua@gmail.com> | 2021-07-15 08:55:08 -0400 |
|---|---|---|
| committer | chirvasitua <chirvasitua@gmail.com> | 2021-07-15 08:55:08 -0400 |
| commit | d7c92b61bd7e0a035ea8ec7f4e515d047982d4a8 (patch) | |
| tree | 91f392ab01861dd2b68740dd12037eb407526c08 /challenge-101/stuart-little/lua | |
| parent | 262314e6f2e38d22bf756d8edac7fbcc5af52e25 (diff) | |
| download | perlweeklychallenge-club-d7c92b61bd7e0a035ea8ec7f4e515d047982d4a8.tar.gz perlweeklychallenge-club-d7c92b61bd7e0a035ea8ec7f4e515d047982d4a8.tar.bz2 perlweeklychallenge-club-d7c92b61bd7e0a035ea8ec7f4e515d047982d4a8.zip | |
1st commit on 101_lua
Diffstat (limited to 'challenge-101/stuart-little/lua')
| -rwxr-xr-x | challenge-101/stuart-little/lua/ch-1.lua | 40 | ||||
| -rwxr-xr-x | challenge-101/stuart-little/lua/ch-2.lua | 16 |
2 files changed, 56 insertions, 0 deletions
diff --git a/challenge-101/stuart-little/lua/ch-1.lua b/challenge-101/stuart-little/lua/ch-1.lua new file mode 100755 index 0000000000..e73bbb0265 --- /dev/null +++ b/challenge-101/stuart-little/lua/ch-1.lua @@ -0,0 +1,40 @@ +#!/usr/bin/env lua + +-- run <script> <space-separated array entries> + +function dims(n) + local rows = 1 + for i=1,math.sqrt(n) do + if n % i == 0 then rows=i end + end + return rows, math.floor(n/rows) +end + +function rot(t) + if #t==0 then return {} end + local rt={} + for i=#(t[1]),1,-1 do + local row={} + for j=1,#t do table.insert(row,t[j][i]) end + table.insert(rt,row) + end + return rt +end + +function spiral(t,rows,cols) + local sp={} + if rows*cols==0 then return sp end + table.insert(sp,table.pack(table.unpack(t,1,cols))) + local rest = rot(spiral(table.pack(table.unpack(t,cols+1)),cols,rows-1)) + for k,v in ipairs(rest) do + table.insert(sp,k,v) + end + return sp +end + +function ppMat(t) + for _,v in ipairs(t) do print(table.unpack(v)) end +end + +local rows,cols=dims(#arg) +ppMat(spiral(arg,rows,cols)) diff --git a/challenge-101/stuart-little/lua/ch-2.lua b/challenge-101/stuart-little/lua/ch-2.lua new file mode 100755 index 0000000000..2f2b02b6ef --- /dev/null +++ b/challenge-101/stuart-little/lua/ch-2.lua @@ -0,0 +1,16 @@ +#!/usr/bin/env lua + +-- run <script> <x1 y1 x2 y2 x3 y3> + +function contains0(t) + local lt90=0 + local tt = table.pack(table.unpack(t)) + for i=1,2 do table.insert(tt,tt[i]) end + for i=1,3 do + local dotP=tt[2*i-1]*tt[2*i+1] + tt[2*i]*tt[2*i+2] + if dotP > 0 then lt90=lt90+1 end + end + return lt90 >= 2 and 0 or 1 +end + +print(contains0(table.pack(table.unpack(arg,1,6)))) |
