Nonoblock: Difference between revisions
Content added Content deleted
Line 1,636: | Line 1,636: | ||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
<lang Lua> |
<lang Lua>local examples = { |
||
-- special for https://rosettacode.org/wiki/Nonoblock |
|||
local examples = { |
|||
{5, {2, 1}}, |
{5, {2, 1}}, |
||
{5, {}}, |
{5, {}}, |
||
Line 1,646: | Line 1,643: | ||
{5, {2, 3}}, |
{5, {2, 3}}, |
||
} |
} |
||
function getFreedom (n, t, tn) |
|||
local fr = n - (#t-tn) -- freedom |
|||
print ('tn', tn) |
|||
for i = tn, #t do |
|||
fr = fr - t[i] |
|||
end |
|||
return fr |
|||
end |
|||
function rep2 (n1, n2, n3) |
|||
return string.rep("0", n1) .. string.rep("1", n2) .. string.rep("0", n3) |
|||
end |
|||
function deep (blocks, iBlock, freedom, str) |
function deep (blocks, iBlock, freedom, str) |
||
if iBlock == #blocks then -- last |
if iBlock == #blocks then -- last |
||
for takenFreedom = 0, freedom do |
for takenFreedom = 0, freedom do |
||
print (str..string.rep("0", takenFreedom) .. string.rep("1", blocks[iBlock]) .. string.rep("0", freedom - takenFreedom)) |
|||
local n = freedom - takenFreedom |
|||
-- print ('n ' .. n) |
|||
local str2 = str..string.rep("0", takenFreedom) .. string.rep("1", blocks[iBlock]) .. string.rep("0", n) |
|||
print (str2) |
|||
total = total + 1 |
total = total + 1 |
||
end |
end |
||
else |
else |
||
for takenFreedom = 0, freedom do |
for takenFreedom = 0, freedom do |
||
-- print ('#blocks '.. #blocks, 'iBlock ' .. iBlock) |
|||
local str2 = str..string.rep("0", takenFreedom) .. string.rep("1", blocks[iBlock]) .. "0" |
local str2 = str..string.rep("0", takenFreedom) .. string.rep("1", blocks[iBlock]) .. "0" |
||
deep (blocks, iBlock+1, freedom-takenFreedom, str2) |
deep (blocks, iBlock+1, freedom-takenFreedom, str2) |