Nonoblock: Difference between revisions

4,995 bytes added ,  4 months ago
m
m (→‎{{header|Wren}}: Minor tidy)
 
(4 intermediate revisions by 3 users not shown)
Line 66:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F nonoblocks([Int] &blocks, Int cells) -> [[(Int, Int)]]
[[(Int, Int)]] r
I blocks.empty | blocks[0] == 0
Line 107:
L(vector) nb
print(‘ ’pblock(vector, cells))
print(‘ A total of #. Possible configurations.’.format(nb.len))</langsyntaxhighlight>
 
{{out}}
Line 156:
 
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">DEFINE MAX_BLOCKS="10"
DEFINE NOT_FOUND="255"
 
Line 270:
Test(15,t4,4)
Test(5,t5,2)
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Nonoblock.png Screenshot from Atari 8-bit computer]
Line 319:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">;-------------------------------------------
NonoBlock(cells, blocks){
result := [], line := ""
Line 393:
return output
}
;-------------------------------------------</langsyntaxhighlight>Examples:<syntaxhighlight lang AutoHotkey="autohotkey">
Results .= NonoBlock(5, [2, 1]) "------------`n"
Results .= NonoBlock(5, []) "------------`n"
Line 401:
MsgBox, 262144, , % Results
return
</syntaxhighlight>
</lang>
{{out}}
<pre>---------------------------
Line 439:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <string.h>
 
Line 501:
 
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 541:
=={{header|C sharp}}==
This solution uses a StringBuilder. Spaces are moved from right to left and the problem is then solved recursively.
<langsyntaxhighlight lang="csharp">using System;
using System.Linq;
using System.Text;
Line 591:
}
 
}</langsyntaxhighlight>
{{out}}
<pre style="height:50ex;overflow:scroll">
Line 628:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">
#include <iomanip>
#include <iostream>
Line 728:
return 0;
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 779:
=={{header|D}}==
{{trans|python}}
<langsyntaxhighlight lang="d">import std.stdio, std.array, std.algorithm, std.exception, std.conv,
std.concurrency, std.range;
 
Line 862:
writeln;
}
}</langsyntaxhighlight>
{{out}}
<pre>Configuration (5 cells and [2, 1] blocks):
Line 960:
 
=={{header|EchoLisp}}==
<langsyntaxhighlight lang="scheme">
;; size is the remaining # of cells
;; blocks is the list of remaining blocks size
Line 1,006:
(writeln "❌ no solution for" size blocks)
(nonoblock size blocks (stack 'cells)))))
</syntaxhighlight>
</lang>
{{out}}
Line 1,049:
=={{header|Elixir}}==
{{trans|Ruby}}
<langsyntaxhighlight lang="elixir">defmodule Nonoblock do
def solve(cell, blocks) do
width = Enum.sum(blocks) + length(blocks) - 1
Line 1,111:
e in RuntimeError -> IO.inspect e
end
end)</langsyntaxhighlight>
 
{{out}}
Line 1,165:
=={{header|Go}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,212:
printBlock("2323", 15)
printBlock("23", 5)
}</langsyntaxhighlight>
 
{{out}}
Line 1,254:
Implementation:
 
<langsyntaxhighlight Jlang="j">nonoblock=:4 :0
s=. 1+(1+x)-+/1+y
pad=.1+(#~ s >+/"1)((1+#y)#s) #: i.s^1+#y
Line 1,260:
)
 
neat=: [: (#~ # $ 0 1"_)@": {&(' ',65}.a.)&.></langsyntaxhighlight>
 
Task example:
 
<langsyntaxhighlight Jlang="j"> neat 5 nonoblock 2 1
│A│A│ │B│ │
│A│A│ │ │B│
Line 1,291:
│ │ │A│A│ │B│B│B│ │C│C│ │D│D│D│
neat 5 nonoblock 2 3
</syntaxhighlight>
</lang>
 
=={{header|Java}}==
{{works with|Java|8}}
<langsyntaxhighlight lang="java">import java.util.*;
import static java.util.Arrays.stream;
import static java.util.stream.Collectors.toList;
Line 1,347:
return sb.toString();
}
}</langsyntaxhighlight>
<pre>blocks [2, 1], cells 5
11010
Line 1,383:
 
=={{header|JavaScript}}==
<langsyntaxhighlight lang="javascript">const compose = (...fn) => (...x) => fn.reduce((a, b) => c => a(b(c)))(...x);
const inv = b => !b;
const arrJoin = str => arr => arr.join(str);
Line 1,444:
test(10, 4, 3);
test(5, 2, 3);
</syntaxhighlight>
</lang>
{{out}}
<pre>5 cells. Blocks: 2,1
Line 1,496:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">minsized(arr) = join(map(x->"#"^x, arr), ".")
minlen(arr) = sum(arr) + length(arr) - 1
Line 1,532:
nonoblocks([2, 3, 2, 3], 15)
nonoblocks([2, 3], 5)
</langsyntaxhighlight> {{output}} <pre>
With blocks [2, 1] and 5 cells:
##.#.
Line 1,565:
=={{header|Kotlin}}==
{{trans|Java}}
<langsyntaxhighlight lang="scala">// version 1.2.0
 
fun printBlock(data: String, len: Int) {
Line 1,597:
printBlock("2323", 15)
printBlock("23", 5)
}</langsyntaxhighlight>
 
{{out}}
Line 1,636:
 
=={{header|Lua}}==
<syntaxhighlight lang="lua">local examples = {
<lang Lua>-- nonoblock
-- special for https://rosettacode.org/wiki/Nonoblock
 
local examples = {
{5, {2, 1}},
{5, {}},
Line 1,646 ⟶ 1,643:
{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)
if iBlock == #blocks then -- last
for takenFreedom = 0, freedom do
-- print ('str..string.rep("0", takenFreedom) '.. takenFreedomstring.rep("1", 'freedom '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
end
else
for takenFreedom = 0, freedom do
-- print ('#blocks '.. #blocks, 'iBlock ' .. iBlock)
local str2 = str..string.rep("0", takenFreedom) .. string.rep("1", blocks[iBlock]) .. "0"
deep (blocks, iBlock+1, freedom-takenFreedom, str2)
Line 1,705 ⟶ 1,682:
main (example[1], example[2])
print ('A total of ' .. total .. ' possible configurations.')
end</langsyntaxhighlight>
 
{{out}}
Line 1,759 ⟶ 1,736:
=={{header|M2000 Interpreter}}==
===Recursive===
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module NonoBlock {
Form 80,40
Line 1,815 ⟶ 1,792:
}
NonoBlock
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,851 ⟶ 1,828:
</pre >
===Non Recursive===
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module Nonoblock (n, m) {
Print "Cells:",n," Blocks:",m
Line 1,942 ⟶ 1,919:
Nonoblock 15,4,2,3,2,3
Nonoblock 5,2,3,2
</syntaxhighlight>
</lang>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[SpacesDistributeOverN, Possibilities]
SpacesDistributeOverN[s_, p_] :=
Flatten[
Line 1,966 ⟶ 1,943:
Possibilities[{8}, 10]
Possibilities[{2, 3, 2, 3}, 15]
Possibilities[{2, 3}, 5]</langsyntaxhighlight>
{{out}}
<pre>{".##.#", "##..#", "##.#."}
Line 1,976 ⟶ 1,953:
=={{header|Nim}}==
{{trans|Go}}
<langsyntaxhighlight Nimlang="nim">import math, sequtils, strformat, strutils
 
 
Line 2,009 ⟶ 1,986:
printBlock("8", 10)
printBlock("2323", 15)
printBlock("23", 5)</langsyntaxhighlight>
 
{{out}}
Line 2,044 ⟶ 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}}==
<langsyntaxhighlight lang="perl">use strict;
use warnings;
 
Line 2,065 ⟶ 2,189:
10 8
15 2 3 2 3
5 2 3</langsyntaxhighlight>
{{out}}
<pre>
Line 2,109 ⟶ 2,233:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="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>
Line 2,159 ⟶ 2,283:
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 2,241 ⟶ 2,365:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">def nonoblocks(blocks, cells):
if not blocks or blocks[0] == 0:
yield [(0, 0)]
Line 2,292 ⟶ 2,416:
for i, vector in enumerate(nonoblocks(blocks, cells)):
print(' ', pblock(vector, cells))
print(' A total of %i Possible configurations.' % (i+1))</langsyntaxhighlight>
 
{{out}}
Line 2,355 ⟶ 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).
 
<langsyntaxhighlight lang="racket">#lang racket
(require racket/trace)
 
Line 2,419 ⟶ 2,543:
(tst 10 '[8])
(tst 15 '[2 3 2 3])
(tst 5 '[2 3]))</langsyntaxhighlight>
 
{{out}}
Line 2,454 ⟶ 2,578:
(formerly Perl 6)
{{trans|Perl}}
<syntaxhighlight lang="raku" perl6line>for (5, [2,1]), (5, []), (10, [8]), (5, [2,3]), (15, [2,3,2,3]) -> ($cells, @blocks) {
say $cells, ' cells with blocks: ', @blocks ?? join ', ', @blocks !! '∅';
my $letter = 'A';
Line 2,462 ⟶ 2,586:
say $row while $row ~~ s/^^ (\.*) <|w> (.*?) <|w> (\w+) \.<!|w> /$1$0.$2/;
say '';
}</langsyntaxhighlight>
{{out}}
<pre>5 cells with blocks: 2, 1
Line 2,498 ⟶ 2,622:
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/*REXX program enumerates all possible configurations (or an error) for nonogram puzzles*/
$.=; $.1= 5 2 1
$.2= 5
Line 2,550 ⟶ 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, .)</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 2,598 ⟶ 2,722:
=={{header|Ruby}}==
'''Simple version:'''
<langsyntaxhighlight lang="ruby">def nonoblocks(cell, blocks)
raise 'Those blocks will not fit in those cells' if cell < blocks.inject(0,:+) + blocks.size - 1
nblock(cell, blocks, '', [])
Line 2,630 ⟶ 2,754:
p e
end
end</langsyntaxhighlight>
 
{{out}}
Line 2,674 ⟶ 2,798:
===Class version===
The output form consulted the one of the python.
<langsyntaxhighlight lang="ruby">class NonoBlock
def initialize(cell, blocks)
raise 'Those blocks will not fit in those cells' if cell < blocks.inject(0,:+) + blocks.size - 1
Line 2,727 ⟶ 2,851:
end
end
end</langsyntaxhighlight>
 
{{out}}
Line 2,781 ⟶ 2,905:
=={{header|Rust}}==
{{works with|Rust|1.29.2}}
<langsyntaxhighlight lang="rust">struct Nonoblock {
width: usize,
config: Vec<usize>,
Line 2,859 ⟶ 2,983:
println!();
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,902 ⟶ 3,026:
 
=={{header|Swift}}==
<langsyntaxhighlight lang="swift">import Foundation
 
func nonoblock(cells: Int, blocks: [Int]) {
Line 2,953 ⟶ 3,077:
print()
 
nonoblock(cells: 5, blocks: [2, 3])</langsyntaxhighlight>
 
{{out}}
Line 2,995 ⟶ 3,119:
{{tcllib|generator}}
{{trans|Python}}
<langsyntaxhighlight lang="tcl">package require Tcl 8.6
package require generator
 
Line 3,062 ⟶ 3,186:
}
 
package provide nonoblock 1</langsyntaxhighlight>
{{out}}
<pre>
Line 3,117 ⟶ 3,241:
{{trans|Kotlin}}
{{libheader|Wren-math}}
<langsyntaxhighlight ecmascriptlang="wren">import "./math" for Nums
 
var genSequence // recursive
Line 3,150 ⟶ 3,274:
printBlock.call("8", 10)
printBlock.call("2323", 15)
printBlock.call("23", 5)</langsyntaxhighlight>
 
{{out}}
Line 3,190 ⟶ 3,314:
=={{header|zkl}}==
{{trans|Python}}
<langsyntaxhighlight lang="zkl">fcn nonoblocks(blocks,cells){
if(not blocks or blocks[0]==0) vm.yield( T(T(0,0)) );
else{
Line 3,222 ⟶ 3,346:
vec.apply2('wrap([(a,b)]){ a.walker(b).pump(Void,vector.set.fp1(ch.next())) });
String("|",vector.concat("|"),"|");
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">foreach blocks,cells in (T( T(T(2,1),5), T(T,5), T(T(8),10), T(T(2,3,2,3),15),
T(T(2,3),5) )){
println("\nConfiguration:\n %s # %d cells and %s blocks"
Line 3,233 ⟶ 3,357:
},0)
: println(" A total of %d possible configurations.".fmt(_));
}</langsyntaxhighlight>
{{out}}
<pre>
9,476

edits