RosettaCodeData/Task/Nonoblock/Phix/nonoblock.phix
2026-02-01 16:33:20 -08:00

49 lines
1.4 KiB
Text

with javascript_semantics
function nobr(sequence res, string neat, integer ni, integer ch, sequence blocks)
if length(blocks)=0 then
res = append(res,neat)
else
integer b = blocks[1]
blocks = blocks[2..$]
integer l = (sum(blocks)+length(blocks)-1)*2,
e = length(neat)-l-b*2
for i=ni to e by 2 do
for j=i to i+b*2-2 by 2 do
neat[j] = ch
end for
res = nobr(res,neat,i+b*2+2,ch+1,blocks)
neat[i] = ' '
end for
end if
return res
end function
function nonoblock(integer len, sequence blocks)
string neat = "|"&join(repeat(' ',len),'|')&"|"
return nobr({},neat,2,'A',blocks)
end function
sequence tests = {{5,{2,1}},
{5,{}},
{10,{8}},
{15,{2, 3, 2, 3}},
{10,{4, 3}},
{5,{2,1}},
{10,{3, 1}},
{5,{2, 3}}}
integer len
sequence blocks, res
for i=1 to length(tests) do
{len,blocks} = tests[i]
string ti = sprintf("%d cells with blocks %s",{len,sprint(blocks)})
printf(1,"%s\n%s\n",{ti,repeat('=',length(ti))})
res = nonoblock(len,blocks)
if length(res)=0 then
printf(1,"No solutions.\n")
else
for ri=1 to length(res) do
printf(1,"%3d: %s\n",{ri,res[ri]})
end for
end if
printf(1,"\n")
end for