Nonoblock: Difference between revisions
m
→{{header|Wren}}: Minor tidy
m (→{{header|Wren}}: Minor tidy) |
|||
(10 intermediate revisions by 8 users not shown) | |||
Line 62:
* The blog post [http://paddy3118.blogspot.co.uk/2014/03/nonogram-puzzle-solver-part-1.html Nonogram puzzle solver (part 1)] Inspired this task and donated its [[Nonoblock#Python]] solution.
<br><br>
=={{header|11l}}==
{{trans|Python}}
<syntaxhighlight lang="11l">F nonoblocks([Int] &blocks, Int cells) -> [[(Int, Int)]]
[[(Int, Int)]] r
I blocks.empty | blocks[0] == 0
r [+]= [(0, 0)]
E
assert(sum(blocks) + blocks.len - 1 <= cells, ‘Those blocks will not fit in those cells’)
V (blength, brest) = (blocks[0], blocks[1..])
V minspace4rest = sum(brest.map(b -> 1 + b))
L(bpos) 0 .. cells - minspace4rest - blength
I brest.empty
r [+]= [(bpos, blength)]
E
V offset = bpos + blength + 1
L(subpos) nonoblocks(&brest, cells - offset)
V rest = subpos.map((bp, bl) -> (@offset + bp, bl))
V vec = [(bpos, blength)] [+] rest
r [+]= vec
R r
F pblock(vec, cells)
‘Prettyprints each run of blocks with a different letter A.. for each block of filled cells’
V vector = [‘_’] * cells
L(bp_bl) vec
V ch = L.index + ‘A’.code
V (bp, bl) = bp_bl
L(i) bp .< bp + bl
vector[i] = I vector[i] == ‘_’ {Char(code' ch)} E Char(‘?’)
R ‘|’vector.join(‘|’)‘|’
L(blocks, cells) [
([2, 1], 5),
([Int](), 5),
([8], 10),
([2, 3, 2, 3], 15)
]
print("\nConfiguration:\n #. ## #. cells and #. blocks".format(pblock([(Int, Int)](), cells), cells, blocks))
print(‘ Possibilities:’)
V nb = nonoblocks(&blocks, cells)
L(vector) nb
print(‘ ’pblock(vector, cells))
print(‘ A total of #. Possible configurations.’.format(nb.len))</syntaxhighlight>
{{out}}
<pre>
Configuration:
|_|_|_|_|_| # 5 cells and [2, 1] blocks
Possibilities:
|A|A|_|B|_|
|A|A|_|_|B|
|_|A|A|_|B|
A total of 3 Possible configurations.
Configuration:
|_|_|_|_|_| # 5 cells and [] blocks
Possibilities:
|_|_|_|_|_|
A total of 1 Possible configurations.
Configuration:
|_|_|_|_|_|_|_|_|_|_| # 10 cells and [8] blocks
Possibilities:
|A|A|A|A|A|A|A|A|_|_|
|_|A|A|A|A|A|A|A|A|_|
|_|_|A|A|A|A|A|A|A|A|
A total of 3 Possible configurations.
Configuration:
|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_| # 15 cells and [2, 3, 2, 3] blocks
Possibilities:
|A|A|_|B|B|B|_|C|C|_|D|D|D|_|_|
|A|A|_|B|B|B|_|C|C|_|_|D|D|D|_|
|A|A|_|B|B|B|_|C|C|_|_|_|D|D|D|
|A|A|_|B|B|B|_|_|C|C|_|D|D|D|_|
|A|A|_|B|B|B|_|_|C|C|_|_|D|D|D|
|A|A|_|B|B|B|_|_|_|C|C|_|D|D|D|
|A|A|_|_|B|B|B|_|C|C|_|D|D|D|_|
|A|A|_|_|B|B|B|_|C|C|_|_|D|D|D|
|A|A|_|_|B|B|B|_|_|C|C|_|D|D|D|
|A|A|_|_|_|B|B|B|_|C|C|_|D|D|D|
|_|A|A|_|B|B|B|_|C|C|_|D|D|D|_|
|_|A|A|_|B|B|B|_|C|C|_|_|D|D|D|
|_|A|A|_|B|B|B|_|_|C|C|_|D|D|D|
|_|A|A|_|_|B|B|B|_|C|C|_|D|D|D|
|_|_|A|A|_|B|B|B|_|C|C|_|D|D|D|
A total of 15 Possible configurations.
</pre>
=={{header|Action!}}==
<syntaxhighlight lang="action!">DEFINE MAX_BLOCKS="10"
DEFINE NOT_FOUND="255"
BYTE FUNC GetBlockAtPos(BYTE p BYTE ARRAY blocks,pos INT count)
INT i
FOR i=0 TO count-1
DO
IF p>=pos(i) AND p<pos(i)+blocks(i) THEN
RETURN (i)
FI
OD
RETURN (NOT_FOUND)
PROC PrintResult(BYTE cells BYTE ARRAY blocks,pos INT count)
BYTE i,b
Print("[")
FOR i=0 TO cells-1
DO
b=GetBlockAtPos(i,blocks,pos,count)
IF b=NOT_FOUND THEN
Put('.)
ELSE
Put(b+'A)
FI
OD
PrintE("]")
RETURN
BYTE FUNC LeftMostPos(BYTE cells BYTE ARRAY blocks,pos INT count,startFrom)
INT i
FOR i=startFrom TO count-1
DO
pos(i)=pos(i-1)+blocks(i-1)+1
IF pos(i)+blocks(i)>cells THEN
RETURN (0)
FI
OD
RETURN (1)
BYTE FUNC MoveToRight(BYTE cells BYTE ARRAY blocks,pos INT count,startFrom)
pos(startFrom)==+1
IF pos(startFrom)+blocks(startFrom)>cells THEN
RETURN (0)
FI
RETURN (LeftMostPos(cells,blocks,pos,count,startFrom+1))
PROC Process(BYTE cells BYTE ARRAY blocks INT count)
BYTE ARRAY pos(MAX_BLOCKS)
BYTE success
INT current
IF count=0 THEN
PrintResult(cells,blocks,pos,count)
RETURN
FI
pos(0)=0
success=LeftMostPos(cells,blocks,pos,count,1)
IF success=0 THEN
PrintE("No solutions")
RETURN
FI
current=count-1
WHILE success
DO
PrintResult(cells,blocks,pos,count)
DO
success=MoveToRight(cells,blocks,pos,count,current)
IF success THEN
current=count-1
ELSE
current==-1
IF current<0 THEN
EXIT
FI
FI
UNTIL success
OD
OD
RETURN
PROC Test(BYTE cells BYTE ARRAY blocks INT count)
BYTE CH=$02FC ;Internal hardware value for last key pressed
INT i
PrintB(cells) Print(" cells [")
FOR i=0 TO count-1
DO
PrintB(blocks(i))
IF i<count-1 THEN
Put(32)
FI
OD
PrintE("]")
Process(cells,blocks,count)
PutE()
PrintE("Press any key to continue...")
DO UNTIL CH#$FF OD
CH=$FF
PutE()
RETURN
PROC Main()
BYTE ARRAY t1=[2 1],t2=[],t3=[8],t4=[2 3 2 3],t5=[2 3]
Test(5,t1,2)
Test(5,t2,0)
Test(10,t3,1)
Test(15,t4,4)
Test(5,t5,2)
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Nonoblock.png Screenshot from Atari 8-bit computer]
<pre>
5 cells [2 1]
[AA.B.]
[AA..B]
[.AA.B]
Press any key to continue...
5 cells []
[.....]
Press any key to continue...
10 cells [8]
[AAAAAAAA..]
[.AAAAAAAA.]
[..AAAAAAAA]
Press any key to continue...
15 cells [2 3 2 3]
[AA.BBB.CC.DDD..]
[AA.BBB.CC..DDD.]
[AA.BBB.CC...DDD]
[AA.BBB..CC.DDD.]
[AA.BBB..CC..DDD]
[AA.BBB...CC.DDD]
[AA..BBB.CC.DDD.]
[AA..BBB.CC..DDD]
[AA..BBB..CC.DDD]
[AA...BBB.CC.DDD]
[.AA.BBB.CC.DDD.]
[.AA.BBB.CC..DDD]
[.AA.BBB..CC.DDD]
[.AA..BBB.CC.DDD]
[..AA.BBB.CC.DDD]
Press any key to continue...
5 cells [2 3]
No solutions
Press any key to continue...
</pre>
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">;-------------------------------------------
NonoBlock(cells, blocks){
result := [], line := ""
for i, v in blocks
B .= v ", "
output := cells " cells and [" Trim(B, ", ") "] blocks`n"
if ((Arr := NonoBlockCreate(cells, blocks)) = "Error")
return output "No Solution`n"
for i, v in arr
line.= v ";"
result[line] := true
result := NonoBlockRecurse(Arr, result)
output .= NonoBlockShow(result)
return output
}
;-------------------------------------------
; create cells+1 size array, stack blocks to left with one gap in between
; gaps are represented by negative number
; stack extra gaps to far left
; for example : 6 cells and [2, 1] blocks
; returns [-2, 2, -1, 1, 0, 0, 0]
NonoBlockCreate(cells, blocks){
Arr := [], B := blocks.Count()
if !B ; no blocks
return [0-cells, 0]
for i, v in blocks{
total += v
Arr.InsertAt(1, blocks[B-A_Index+1])
Arr.InsertAt(1, -1)
}
if (cells < total + B-1) ; not possible
return "Error"
Arr[1] := total + B-1 - cells
loop % cells - Arr.Count() + 1
Arr.Push(0)
return Arr
}
;-------------------------------------------
; shift negative numbers from left to right recursively.
; preserve at least one gap between blocks.
; [-2, 2, -1, 1, 0, 0, 0]
; [-1, 2, -2, 1, 0, 0, 0]
NonoBlockRecurse(Arr, result, pos:= 1){
i := pos-1
while (i < Arr.count())
{
if ((B:=Arr[++i])>=0) || (B=-1 && i>1)
continue
if (i=Arr.count()-1)
return result
Arr[i] := ++B, Arr[i+2] := Arr[i+2] -1
result := NonoBlockRecurse(Arr.Clone(), result, i)
line := []
for k, v in Arr
line.=v ";"
result[line] := true
}
return result
}
;-------------------------------------------
; represent positve numbers by a block of "#", negative nubmers by a block of "."
NonoBlockShow(result){
for line in result{
i := A_Index
nLine := ""
for j, val in StrSplit(line, ";")
loop % Abs(val)
nLine .= val > 0 ? "#" : "."
output .= nLine "`n"
}
Sort, output, U
return output
}
;-------------------------------------------</syntaxhighlight>Examples:<syntaxhighlight lang="autohotkey">
Results .= NonoBlock(5, [2, 1]) "------------`n"
Results .= NonoBlock(5, []) "------------`n"
Results .= NonoBlock(10, [8]) "------------`n"
Results .= NonoBlock(15, [2, 3, 2, 3]) "------------`n"
Results .= NonoBlock(5, [2, 3]) "------------`n"
MsgBox, 262144, , % Results
return
</syntaxhighlight>
{{out}}
<pre>---------------------------
5 cells and [2, 1] blocks
##.#.
##..#
.##.#
------------
5 cells and [] blocks
.....
------------
10 cells and [8] blocks
########..
.########.
..########
------------
15 cells and [2, 3, 2, 3] blocks
##.###.##.###..
##.###.##..###.
##.###.##...###
##.###..##.###.
##.###..##..###
##.###...##.###
##..###.##.###.
##..###.##..###
##..###..##.###
##...###.##.###
.##.###.##.###.
.##.###.##..###
.##.###..##.###
.##..###.##.###
..##.###.##.###
------------
5 cells and [2, 3] blocks
No Solution
------------</pre>
=={{header|C}}==
<
#include <string.h>
Line 126 ⟶ 501:
return 0;
}</
{{out}}
Line 166 ⟶ 541:
=={{header|C sharp}}==
This solution uses a StringBuilder. Spaces are moved from right to left and the problem is then solved recursively.
<
using System.Linq;
using System.Text;
Line 216 ⟶ 591:
}
}</
{{out}}
<pre style="height:50ex;overflow:scroll">
Line 253 ⟶ 628:
=={{header|C++}}==
<
#include <iomanip>
#include <iostream>
Line 353 ⟶ 728:
return 0;
}
</syntaxhighlight>
{{out}}
<pre>
Line 404 ⟶ 779:
=={{header|D}}==
{{trans|python}}
<
std.concurrency, std.range;
Line 487 ⟶ 862:
writeln;
}
}</
{{out}}
<pre>Configuration (5 cells and [2, 1] blocks):
Line 585 ⟶ 960:
=={{header|EchoLisp}}==
<
;; size is the remaining # of cells
;; blocks is the list of remaining blocks size
Line 631 ⟶ 1,006:
(writeln "❌ no solution for" size blocks)
(nonoblock size blocks (stack 'cells)))))
</syntaxhighlight>
{{out}}
Line 674 ⟶ 1,049:
=={{header|Elixir}}==
{{trans|Ruby}}
<
def solve(cell, blocks) do
width = Enum.sum(blocks) + length(blocks) - 1
Line 736 ⟶ 1,111:
e in RuntimeError -> IO.inspect e
end
end)</
{{out}}
Line 790 ⟶ 1,165:
=={{header|Go}}==
{{trans|Kotlin}}
<
import (
Line 837 ⟶ 1,212:
printBlock("2323", 15)
printBlock("23", 5)
}</
{{out}}
Line 879 ⟶ 1,254:
Implementation:
<
s=. 1+(1+x)-+/1+y
pad=.1+(#~ s >+/"1)((1+#y)#s) #: i.s^1+#y
Line 885 ⟶ 1,260:
)
neat=: [: (#~ # $ 0 1"_)@": {&(' ',65}.a.)&.></
Task example:
<
│A│A│ │B│ │
│A│A│ │ │B│
Line 916 ⟶ 1,291:
│ │ │A│A│ │B│B│B│ │C│C│ │D│D│D│
neat 5 nonoblock 2 3
</syntaxhighlight>
=={{header|Java}}==
{{works with|Java|8}}
<
import static java.util.Arrays.stream;
import static java.util.stream.Collectors.toList;
Line 972 ⟶ 1,347:
return sb.toString();
}
}</
<pre>blocks [2, 1], cells 5
11010
Line 1,008 ⟶ 1,383:
=={{header|JavaScript}}==
<
const inv = b => !b;
const arrJoin = str => arr => arr.join(str);
Line 1,069 ⟶ 1,444:
test(10, 4, 3);
test(5, 2, 3);
</syntaxhighlight>
{{out}}
<pre>5 cells. Blocks: 2,1
Line 1,121 ⟶ 1,496:
=={{header|Julia}}==
<
minlen(arr) = sum(arr) + length(arr) - 1
Line 1,157 ⟶ 1,532:
nonoblocks([2, 3, 2, 3], 15)
nonoblocks([2, 3], 5)
</
With blocks [2, 1] and 5 cells:
##.#.
Line 1,190 ⟶ 1,565:
=={{header|Kotlin}}==
{{trans|Java}}
<
fun printBlock(data: String, len: Int) {
Line 1,222 ⟶ 1,597:
printBlock("2323", 15)
printBlock("23", 5)
}</
{{out}}
Line 1,258 ⟶ 1,633:
blocks [2, 3], cells 5
No solution
</pre>
=={{header|Lua}}==
<syntaxhighlight lang="lua">local examples = {
{5, {2, 1}},
{5, {}},
{10, {8}},
{15, {2, 3, 2, 3}},
{5, {2, 3}},
}
function deep (blocks, iBlock, freedom, str)
if iBlock == #blocks then -- last
for takenFreedom = 0, freedom do
print (str..string.rep("0", takenFreedom) .. string.rep("1", blocks[iBlock]) .. string.rep("0", freedom - takenFreedom))
total = total + 1
end
else
for takenFreedom = 0, freedom do
local str2 = str..string.rep("0", takenFreedom) .. string.rep("1", blocks[iBlock]) .. "0"
deep (blocks, iBlock+1, freedom-takenFreedom, str2)
end
end
end
function main (cells, blocks) -- number, list
local str = " "
print (cells .. ' cells and {' .. table.concat(blocks, ', ') .. '} blocks')
local freedom = cells - #blocks + 1 -- freedom
for iBlock = 1, #blocks do
freedom = freedom - blocks[iBlock]
end
if #blocks == 0 then
print ('no blocks')
print (str..string.rep("0", cells))
total = 1
elseif freedom < 0 then
print ('no solutions')
else
print ('Possibilities:')
deep (blocks, 1, freedom, str)
end
end
for i, example in ipairs (examples) do
print ("\n--")
total = 0
main (example[1], example[2])
print ('A total of ' .. total .. ' possible configurations.')
end</syntaxhighlight>
{{out}}
<pre>
--
5 cells and {2, 1} blocks
Possibilities:
11010
11001
01101
A total of 3 possible configurations.
--
5 cells and {} blocks
no blocks
00000
A total of 1 possible configurations.
--
10 cells and {8} blocks
Possibilities:
1111111100
0111111110
0011111111
A total of 3 possible configurations.
--
15 cells and {2, 3, 2, 3} blocks
Possibilities:
110111011011100
110111011001110
110111011000111
110111001101110
110111001100111
110111000110111
110011101101110
110011101100111
110011100110111
110001110110111
011011101101110
011011101100111
011011100110111
011001110110111
001101110110111
A total of 15 possible configurations.
--
5 cells and {2, 3} blocks
no solutions
A total of 0 possible configurations.
</pre>
=={{header|M2000 Interpreter}}==
===Recursive===
<syntaxhighlight lang="m2000 interpreter">
Module NonoBlock {
Form 80,40
Line 1,318 ⟶ 1,792:
}
NonoBlock
</syntaxhighlight>
{{out}}
Line 1,354 ⟶ 1,828:
</pre >
===Non Recursive===
<syntaxhighlight lang="m2000 interpreter">
Module Nonoblock (n, m) {
Print "Cells:",n," Blocks:",m
Line 1,445 ⟶ 1,919:
Nonoblock 15,4,2,3,2,3
Nonoblock 5,2,3,2
</syntaxhighlight>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">ClearAll[SpacesDistributeOverN, Possibilities]
SpacesDistributeOverN[s_, p_] :=
Flatten[
Permutations /@ (Join[#, ConstantArray[0, p - Length[#]]] & /@
IntegerPartitions[s, p]), 1]
Possibilities[hint_, len_] :=
Module[{p = hint, l = len, b = Length[hint], Spaces, out},
Spaces = # + (Prepend[Append[ConstantArray[1, b - 1], 0],
0]) & /@ (SpacesDistributeOverN[l - Total@p - (b - 1), b + 1]);
out = Flatten /@ (
Riffle[#, Map[Table[1, {#}] &, p, {1}]] & /@
Map[Table[0, {#}] &, Spaces, {2}]);
StringJoin @@@ (out /. {0 -> ".", 1 -> "#"})
]
Possibilities[{}, len_] := Module[{},
{StringJoin[ConstantArray[".", len]]}
]
Possibilities[{2, 1}, 5]
Possibilities[{}, 5]
Possibilities[{8}, 10]
Possibilities[{2, 3, 2, 3}, 15]
Possibilities[{2, 3}, 5]</syntaxhighlight>
{{out}}
<pre>{".##.#", "##..#", "##.#."}
{"....."}
{"..########", "########..", ".########."}
{"..##.###.##.###", "##...###.##.###", "##.###...##.###", "##.###.##...###", "##.###.##.###..", ".##..###.##.###", ".##.###..##.###", ".##.###.##..###", ".##.###.##.###.", "##..###..##.###", "##..###.##..###", "##..###.##.###.", "##.###..##..###", "##.###..##.###.", "##.###.##..###."}
{}</pre>
=={{header|Nim}}==
{{trans|Go}}
<
Line 1,482 ⟶ 1,986:
printBlock("8", 10)
printBlock("2323", 15)
printBlock("23", 5)</
{{out}}
Line 1,517 ⟶ 2,021:
blocks [2, 3] cells 5
No solution</pre>
=={{header|Pascal}}==
A console application in Free Pascal, created with the Lazarus IDE.
With 15 cells and [2,3,2,3] blocks, it's a question of how to distribute 5 gap characters among 5 gaps (including the 2 gaps at the ends). To allow for the requirement that the 3 inner gaps must be strictly positive, we can reduce the size of each inner gap by 1, provided we remember to restore the deleted gap character when printing the result. Then 2 gap characters need to be distributed among 5 non-negative gaps. In general, for integers n > 0 and s, the task amounts to finding all arrays of n non-negative integers that sum to s. An iterative method is shown below.
<syntaxhighlight lang="pascal">
program Nonoblock;
uses SysUtils;
// Working through solutions to the problem:
// Fill an array z[] with non-negative integers
// whose sum is the passed-in integer s.
function GetFirstSolution( var z : array of integer;
s : integer) : boolean;
var
j : integer;
begin
result := (s >= 0) and (High(z) >= 0); // failed if s < 0 or array is empty
if result then begin // else initialize to solution 0, ..., 0, s
j := High(z); z[j] := s;
while (j > 0) do begin
dec(j); z[j] := 0;
end;
end;
end;
// Next solution: return true for success, false if no more solutions.
// Solutions are generated in lexicographic order.
function GetNextSolution( var z : array of integer) : boolean;
var
h, j : integer;
begin
h := High(z);
j := h; // find highest index j such that z[j] > 0.
while (j > 0) and (z[j] = 0) do dec(j);
result := (j > 0); // if index is 0, or there is no such index, failed
if result then begin // else update caller's array to give next solution
inc(z[j - 1]);
z[h] := z[j] - 1;
if (j < h) then z[j] := 0;
end;
end;
// Procedure to print solutions to nonoblock task on RosettaCode
procedure PrintSolutions( nrCells : integer;
blockSizes : array of integer);
const // cosmetic
MARGIN = 4;
GAP_CHAR = '.';
BLOCK_CHAR = '#';
var
sb : SysUtils.TStringBuilder;
nrBlocks, blockSum, gapSum : integer;
gapSizes : array of integer;
i, nrSolutions : integer;
begin
nrBlocks := Length( blockSizes);
// Print a title, showing the number of cells and the block sizes
sb := SysUtils.TStringBuilder.Create();
sb.AppendFormat('%d cells; blocks [', [nrCells]);
for i := 0 to nrBlocks - 1 do begin
if (i > 0) then sb.Append(',');
sb.Append( blockSizes[i]);
end;
sb.Append(']');
WriteLn( sb.ToString());
blockSum := 0; // total of block sizes
for i := 0 to nrBlocks - 1 do inc( blockSum, blockSizes[i]);
gapSum := nrCells - blockSum;
// Except in the trivial case of no blocks,
// we reduce the size of each inner gap by 1.
if nrBlocks > 0 then dec( gapSum, nrBlocks - 1);
// Work through all solutions and print them nicely.
nrSolutions := 0;
SetLength( gapSizes, nrBlocks + 1); // include the gap at each end
if GetFirstSolution( gapSizes, gapSum) then begin
repeat
inc( nrSolutions);
sb.Clear();
sb.Append( ' ', MARGIN);
for i := 0 to nrBlocks - 1 do begin
sb.Append( GAP_CHAR, gapSizes[i]);
// We reduced the inner gaps by 1; now we restore the deleted char.
if (i > 0) then sb.Append( GAP_CHAR);
sb.Append( BLOCK_CHAR, blockSizes[i]);
end;
sb.Append( GAP_CHAR, gapSizes[nrBlocks]);
WriteLn( sb.ToString());
until not GetNextSolution( gapSizes);
end;
sb.Free();
WriteLn( SysUtils.Format( 'Number of solutions = %d', [nrSolutions]));
WriteLn('');
end;
// Main program
begin
PrintSolutions( 5, [2,1]);
PrintSolutions( 5, []);
PrintSolutions( 10, [8]);
PrintSolutions( 15, [2,3,2,3]);
PrintSolutions( 5, [2,3]);
end.
</syntaxhighlight>
{{out}}
<pre>
5 cells; blocks [2,1]
##.#.
##..#
.##.#
Number of solutions = 3
5 cells; blocks []
.....
Number of solutions = 1
10 cells; blocks [8]
########..
.########.
..########
Number of solutions = 3
15 cells; blocks [2,3,2,3]
##.###.##.###..
##.###.##..###.
##.###.##...###
##.###..##.###.
##.###..##..###
##.###...##.###
##..###.##.###.
##..###.##..###
##..###..##.###
##...###.##.###
.##.###.##.###.
.##.###.##..###
.##.###..##.###
.##..###.##.###
..##.###.##.###
Number of solutions = 15
5 cells; blocks [2,3]
Number of solutions = 0
</pre>
=={{header|Perl}}==
<
use warnings;
Line 1,538 ⟶ 2,189:
10 8
15 2 3 2 3
5 2 3</
{{out}}
<pre>
Line 1,582 ⟶ 2,233:
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">nobr</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">string</span> <span style="color: #000000;">neat</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">ni</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">ch</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">sequence</span> <span style="color: #000000;">blocks</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">blocks</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">neat</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">else</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">b</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">blocks</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #000000;">blocks</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">blocks</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">..$]</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">l</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">(</span><span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #000000;">blocks</span><span style="color: #0000FF;">)+</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">blocks</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)*</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">e</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">neat</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">l</span><span style="color: #0000FF;">-</span><span style="color: #000000;">b</span><span style="color: #0000FF;">*</span><span style="color: #000000;">2</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">ni</span> <span style="color: #008080;">to</span> <span style="color: #000000;">e</span> <span style="color: #008080;">by</span> <span style="color: #000000;">2</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">i</span> <span style="color: #008080;">to</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">b</span><span style="color: #0000FF;">*</span><span style="color: #000000;">2</span><span style="color: #0000FF;">-</span><span style="color: #000000;">2</span> <span style="color: #008080;">by</span> <span style="color: #000000;">2</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">neat</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ch</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">nobr</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">neat</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">b</span><span style="color: #0000FF;">*</span><span style="color: #000000;">2</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ch</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">blocks</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">neat</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">' '</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">nonoblock</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">len</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">sequence</span> <span style="color: #000000;">blocks</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">neat</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"|"</span><span style="color: #0000FF;">&</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">' '</span><span style="color: #0000FF;">,</span><span style="color: #000000;">len</span><span style="color: #0000FF;">),</span><span style="color: #008000;">'|'</span><span style="color: #0000FF;">)&</span><span style="color: #008000;">"|"</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">nobr</span><span style="color: #0000FF;">({},</span><span style="color: #000000;">neat</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #008000;">'A'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">blocks</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">tests</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,{}},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">8</span><span style="color: #0000FF;">}},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">15</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">}},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">}},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">}},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">}}}</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">len</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">blocks</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">res</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">len</span><span style="color: #0000FF;">,</span><span style="color: #000000;">blocks</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tests</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">ti</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%d cells with blocks %s"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">len</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sprint</span><span style="color: #0000FF;">(</span><span style="color: #000000;">blocks</span><span style="color: #0000FF;">)})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s\n%s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">ti</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'='</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ti</span><span style="color: #0000FF;">))})</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">nonoblock</span><span style="color: #0000FF;">(</span><span style="color: #000000;">len</span><span style="color: #0000FF;">,</span><span style="color: #000000;">blocks</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"No solutions.\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">else</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">ri</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%3d: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">ri</span><span style="color: #0000FF;">,</span><span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">ri</span><span style="color: #0000FF;">]})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 1,711 ⟶ 2,365:
=={{header|Python}}==
<
if not blocks or blocks[0] == 0:
yield [(0, 0)]
Line 1,762 ⟶ 2,416:
for i, vector in enumerate(nonoblocks(blocks, cells)):
print(' ', pblock(vector, cells))
print(' A total of %i Possible configurations.' % (i+1))</
{{out}}
Line 1,825 ⟶ 2,479:
Also, the blocks are not identified. I suppose they could be easily enough, but in the nonogram task, these patterns are converted to bit-fields shortly after the nonoblock generation, and bits have no names (sad, but true).
<
(require racket/trace)
Line 1,889 ⟶ 2,543:
(tst 10 '[8])
(tst 15 '[2 3 2 3])
(tst 5 '[2 3]))</
{{out}}
Line 1,924 ⟶ 2,578:
(formerly Perl 6)
{{trans|Perl}}
<syntaxhighlight lang="raku"
say $cells, ' cells with blocks: ', @blocks ?? join ', ', @blocks !! '∅';
my $letter = 'A';
Line 1,932 ⟶ 2,586:
say $row while $row ~~ s/^^ (\.*) <|w> (.*?) <|w> (\w+) \.<!|w> /$1$0.$2/;
say '';
}</
{{out}}
<pre>5 cells with blocks: 2, 1
Line 1,968 ⟶ 2,622:
=={{header|REXX}}==
<
$.=; $.1= 5 2 1
$.2= 5
Line 2,020 ⟶ 2,674:
loc: _=0; do arg(2); _=pos('#.',pad(arg(1)),_+1); if _==0 then return 0; end; return _+1
add: if !.new==1 then return; #= # + 1; @.#= new; !.new=1; return
pad: return left( arg(1), N, .)</
{{out|output|text= when using the default inputs:}}
<pre>
Line 2,068 ⟶ 2,722:
=={{header|Ruby}}==
'''Simple version:'''
<
raise 'Those blocks will not fit in those cells' if cell < blocks.inject(0,:+) + blocks.size - 1
nblock(cell, blocks, '', [])
Line 2,100 ⟶ 2,754:
p e
end
end</
{{out}}
Line 2,144 ⟶ 2,798:
===Class version===
The output form consulted the one of the python.
<
def initialize(cell, blocks)
raise 'Those blocks will not fit in those cells' if cell < blocks.inject(0,:+) + blocks.size - 1
Line 2,197 ⟶ 2,851:
end
end
end</
{{out}}
Line 2,251 ⟶ 2,905:
=={{header|Rust}}==
{{works with|Rust|1.29.2}}
<
width: usize,
config: Vec<usize>,
Line 2,329 ⟶ 2,983:
println!();
}
}</
{{out}}
<pre>
Line 2,372 ⟶ 3,026:
=={{header|Swift}}==
<
func nonoblock(cells: Int, blocks: [Int]) {
Line 2,423 ⟶ 3,077:
print()
nonoblock(cells: 5, blocks: [2, 3])</
{{out}}
Line 2,465 ⟶ 3,119:
{{tcllib|generator}}
{{trans|Python}}
<
package require generator
Line 2,532 ⟶ 3,186:
}
package provide nonoblock 1</
{{out}}
<pre>
Line 2,587 ⟶ 3,241:
{{trans|Kotlin}}
{{libheader|Wren-math}}
<
var genSequence // recursive
Line 2,620 ⟶ 3,274:
printBlock.call("8", 10)
printBlock.call("2323", 15)
printBlock.call("23", 5)</
{{out}}
Line 2,660 ⟶ 3,314:
=={{header|zkl}}==
{{trans|Python}}
<
if(not blocks or blocks[0]==0) vm.yield( T(T(0,0)) );
else{
Line 2,692 ⟶ 3,346:
vec.apply2('wrap([(a,b)]){ a.walker(b).pump(Void,vector.set.fp1(ch.next())) });
String("|",vector.concat("|"),"|");
}</
<
T(T(2,3),5) )){
println("\nConfiguration:\n %s # %d cells and %s blocks"
Line 2,703 ⟶ 3,357:
},0)
: println(" A total of %d possible configurations.".fmt(_));
}</
{{out}}
<pre>
|