How to resolve the algorithm Nonoblock step by step in the Wren programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Nonoblock step by step in the Wren programming language

Table of Contents

Problem Statement

Nonoblock is a chip off the old Nonogram puzzle.

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: And would expand to the following 3 possible rows of block positions:

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:

(This is the algorithm used in the Nonoblock#Python solution).

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Nonoblock step by step in the Wren programming language

Source code in the wren programming language

import "/math" for Nums

var genSequence // recursive
genSequence = Fn.new { |ones, numZeros|
    if (ones.isEmpty) return ["0" * numZeros]
    var result = []
    for (x in 1...numZeros - ones.count + 2) {
        var skipOne = ones[1..-1]
        for (tail in genSequence.call(skipOne, numZeros - x)) {
            result.add("0" * x + ones[0] + tail)
        }
    }
    return result
}

var printBlock = Fn.new { |data, len|
    var a = data.toList
    var sumChars = Nums.sum(a.map { |c| c.bytes[0] - 48 }.toList)
    System.print("\nblocks %(a), cells %(len)")
    if (len - sumChars <= 0) {
        System.print("No solution")
        return
    }
    var prep = a.map { |c| "1" * (c.bytes[0] - 48) }.toList
    for (r in genSequence.call(prep, len - sumChars + 1)) {
        System.print(r[1..-1])
    }
}

printBlock.call("21", 5)
printBlock.call("", 5)
printBlock.call("8", 10)
printBlock.call("2323", 15)
printBlock.call("23", 5)

  

You may also check:How to resolve the algorithm Greatest element of a list step by step in the Vim Script programming language
You may also check:How to resolve the algorithm Repeat a string step by step in the PowerShell programming language
You may also check:How to resolve the algorithm Enforced immutability step by step in the REXX programming language
You may also check:How to resolve the algorithm Primality by Wilson's theorem step by step in the Pascal programming language
You may also check:How to resolve the algorithm Ackermann function step by step in the Comal programming language