Nonoblock: Difference between revisions

Content added Content deleted
m (→‎{{header|Wren}}: Minor tidy)
 
Line 1,493: Line 1,493:




</pre>

=={{header|jq}}==
'''Adapted from [[#Wren|Wren]]'''

'''Works with jq, the C implementation of jq'''

'''Works with gojq, the Go implementation of jq'''

'''Works with jaq, the Rust implementation of jq'''
<syntaxhighlight lang="jq">
def sum(stream): reduce stream as $x (0; . + $x);

def genSequence($ones; $numZeros):
if $ones|length == 0 then "." * $numZeros
else range(1; $numZeros - ($ones|length) + 2) as $x
| genSequence($ones[1:]; $numZeros - $x) as $tail
| "." * $x + $ones[0] + $tail
end;

def printBlock($data; $len):
sum($data | explode[] | . - 48) as $sumChars
| "\nblocks \($data), cells \($len)",
(if $len - $sumChars <= 0
then "No solution"
else ( $data | explode | map( "1" * (. - 48) ) ) as $prep
| genSequence($prep; $len - $sumChars + 1)[1:]
end) ;

printBlock( "21"; 5),
printBlock( ""; 5),
printBlock( "8"; 10),
printBlock("2323"; 15),
printBlock( "23"; 5)
</syntaxhighlight>
{{output}}
<pre>
blocks 21, cells 5
11.1.
11..1
.11.1

blocks , cells 5
.....

blocks 8, cells 10
11111111..
.11111111.
..11111111

blocks 2323, cells 15
11.111.11.111..
11.111.11..111.
11.111.11...111
11.111..11.111.
11.111..11..111
11.111...11.111
11..111.11.111.
11..111.11..111
11..111..11.111
11...111.11.111
.11.111.11.111.
.11.111.11..111
.11.111..11.111
.11..111.11.111
..11.111.11.111

blocks 23, cells 5
No solution
</pre>
</pre>