aboutsummaryrefslogtreecommitdiff
path: root/challenge-101/stuart-little/lua
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-101/stuart-little/lua')
-rwxr-xr-xchallenge-101/stuart-little/lua/ch-1.lua40
-rwxr-xr-xchallenge-101/stuart-little/lua/ch-2.lua16
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))))