aboutsummaryrefslogtreecommitdiff
path: root/challenge-191/deadmarshal/lua/ch-2.lua
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-191/deadmarshal/lua/ch-2.lua')
-rw-r--r--challenge-191/deadmarshal/lua/ch-2.lua28
1 files changed, 28 insertions, 0 deletions
diff --git a/challenge-191/deadmarshal/lua/ch-2.lua b/challenge-191/deadmarshal/lua/ch-2.lua
new file mode 100644
index 0000000000..0c6131362a
--- /dev/null
+++ b/challenge-191/deadmarshal/lua/ch-2.lua
@@ -0,0 +1,28 @@
+local function is_cute(t)
+ assert(type(t) == 'table', 't must be a table!')
+ for i=1, #t do
+ if i % t[i] ~= 0 and t[i] % i ~= 0 then return false end
+ end
+ return true
+end
+
+local function permute(t, i, sz, t2)
+ assert(type(t) == 'table' and
+ type(i) == 'number' and
+ type(sz) == 'number' and
+ type(t2) == 'table', 'params assertions failed!')
+ if sz == i then
+ if is_cute(t) then t2.count = t2.count + 1 return end
+ end
+ for j=i, sz do
+ t[i], t[j] = t[j], t[i]
+ permute(t,i+1,sz,t2)
+ t[i], t[j] = t[j], t[i]
+ end
+ return
+end
+
+local t,t2 = {1,2},{count = 0}
+permute(t, 1, 2, t2)
+print(string.format("%d",t2.count))
+