Nonoblock is a chip off the old Nonogram puzzle.



Given

 The number of cells in a row.
 The size of each, (space separated), connected block of cells to fit in the row, in left-to right order.


Task
 
 show all possible positions. 
 show the number of positions of the blocks for the following cases within the row. 
 show all output on this page. 
 use a "neat" diagram of the block positions.


Enumerate the following configurations

   5   cells   and   [2, 1]   blocks
   5   cells   and   []   blocks   (no blocks)
   10   cells   and   [8]   blocks
   15   cells   and   [2, 3, 2, 3]   blocks
   5   cells   and   [2, 3]   blocks   (should give some indication of this not being possible)


Example

Given a row of five cells and a block of two cells followed by a block of one cell - in that order, the example could be shown as:

   |_|_|_|_|_| # 5 cells and [2, 1] blocks

And would expand to the following 3 possible rows of block positions:

   |A|A|_|B|_|
   |A|A|_|_|B|
   |_|A|A|_|B|



Note how the sets of blocks are always separated by a space.

Note also that it is not necessary for each block to have a separate letter.
Output approximating

This:

                       |#|#|_|#|_|
                       |#|#|_|_|#|
                       |_|#|#|_|#|

This would also work:

                       ##.#.
                       ##..#
                      .##.#



An algorithm

 Find the minimum space to the right that is needed to legally hold all but the leftmost block of cells (with a space between blocks remember).
 The leftmost cell can legitimately be placed in all positions from the LHS up to a RH position that allows enough room for the rest of the blocks.
 for each position of the LH block recursively compute the position of the rest of the blocks in the remaining space to the right of the current placement of the LH block.
(This is the algorithm used in the Nonoblock#Python solution).



Reference

 The blog post Nonogram puzzle solver (part 1) Inspired this task and donated its Nonoblock#Python solution.


import std.stdio, std.array, std.algorithm, std.exception, std.conv,
       std.concurrency, std.range;
 
struct Solution { uint pos, len; }
 
Generator!(Solution[]) nonoBlocks(in uint[] blocks, in uint cells) {
    return new typeof(return)({
        if (blocks.empty || blocks[0] == 0) {
            yield([Solution(0, 0)]);
        } else {
            enforce(blocks.sum + blocks.length - 1 <= cells,
                    "Those blocks cannot fit in those cells.");
            immutable firstBl = blocks[0];
            const restBl = blocks.dropOne;
 
            // The other blocks need space.
            immutable minS = restBl.map!(b => b + 1).sum;
 
            // Slide the start position from left to max RH
            // index allowing for other blocks.
            foreach (immutable bPos; 0 .. cells - minS - firstBl + 1) {
                if (restBl.empty) {
                    // No other blocks to the right so just yield
                    // this one.
                    yield([Solution(bPos, firstBl)]);
                } else {
                    // More blocks to the right so create a sub-problem
                    // of placing the restBl blocks in the cells one
                    // space to the right of the RHS of this block.
                    immutable offset = bPos + firstBl + 1;
                    immutable newCells = cells - offset;
 
                    // Recursive call to nonoBlocks yields multiple
                    // sub-positions.
                    foreach (const subPos; nonoBlocks(restBl, newCells)) {
                        // Remove the offset from sub block positions.
                        auto rest = subPos.map!(sol => Solution(offset + sol.pos, sol.len));
 
                        // Yield this block plus sub blocks positions.
                        yield(Solution(bPos, firstBl) ~ rest.array);
                    }
                }
            }
        }
    });
}
 
 
/// Pretty prints each run of blocks with a
/// different letter for each block of filled cells.
string show(in Solution[] vec, in uint nCells) pure {
    auto result = ['_'].replicate(nCells);
    foreach (immutable i, immutable sol; vec)
        foreach (immutable j; sol.pos .. sol.pos + sol.len)
            result[j] = (result[j] == '_') ? to!char('A' + i) : '?';
    return '[' ~ result ~ ']';
}
 
void main() {
    static struct Problem { uint[] blocks; uint nCells; }
 
    immutable Problem[] problems = [{[2, 1], 5},
                                    {[], 5},
                                    {[8], 10},
                                    {[2, 3, 2, 3], 15},
                                    {[4, 3], 10},
                                    {[2, 1], 5},
                                    {[3, 1], 10},
                                    {[2, 3], 5}];
 
    foreach (immutable prob; problems) {
        writefln("Configuration (%d cells and %s blocks):",
                 prob.nCells, prob.blocks);
        show([], prob.nCells).writeln;
        "Possibilities:".writeln;
        auto nConfigs = 0;
        foreach (const sol; nonoBlocks(prob.tupleof)) {
            show(sol, prob.nCells).writeln;
            nConfigs++;
        }
        writefln("A total of %d possible configurations.", nConfigs);
        writeln;
    }
}