aboutsummaryrefslogtreecommitdiff
path: root/challenge-191/roger-bell-west/lua/ch-2.lua
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-191/roger-bell-west/lua/ch-2.lua')
-rwxr-xr-xchallenge-191/roger-bell-west/lua/ch-2.lua73
1 files changed, 73 insertions, 0 deletions
diff --git a/challenge-191/roger-bell-west/lua/ch-2.lua b/challenge-191/roger-bell-west/lua/ch-2.lua
new file mode 100755
index 0000000000..f05338a714
--- /dev/null
+++ b/challenge-191/roger-bell-west/lua/ch-2.lua
@@ -0,0 +1,73 @@
+#! /usr/bin/lua
+
+function cutelist(n)
+ local tab = {}
+ local tmp = {}
+ for x = 1,n do
+ local row = {}
+ for y = 1,n do
+ table.insert(row, false)
+ end
+ table.insert(tab, row)
+ table.insert(tmp, x)
+ end
+ for x = 1,n do
+ for y = 1,x do
+ if x % y ~= 0 and y % x ~= 0 then
+ tab[x][y] = true
+ tab[y][x] = true
+ end
+ end
+ end
+ local count = 0
+ local stackl = {{}}
+ local stackc = {tmp}
+ while #stackl > 0 do
+ local l = table.remove(stackl)
+ local c = table.remove(stackc)
+ if #c == 0 and #l == n then
+ count = count + 1
+ else
+ local place = #l + 1
+ for i, candidate in ipairs(c) do
+ if not tab[place][candidate] then
+ local ql = {}
+ for j, qx in ipairs(l) do
+ table.insert(ql, qx)
+ end
+ table.insert(ql, candidate)
+ table.insert(stackl, ql)
+ local qc = {}
+ for j, qx in ipairs(c) do
+ if qx ~= candidate then
+ table.insert(qc, qx)
+ end
+ end
+ table.insert(stackc, qc)
+ end
+ end
+ end
+ end
+ return count
+end
+
+if cutelist(2) == 2 then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+io.write(" ")
+
+if cutelist(10) == 700 then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+io.write(" ")
+
+if cutelist(15) == 24679 then
+ io.write("Pass")
+else
+ io.write("FAIL")
+end
+print("")