diff options
Diffstat (limited to 'challenge-101/stuart-little/lua/ch-1.lua')
| -rwxr-xr-x | challenge-101/stuart-little/lua/ch-1.lua | 40 |
1 files changed, 40 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)) |
