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

Published on 22 June 2024 08:30 PM

How to resolve the algorithm Nonoblock step by step in the Julia 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 Julia programming language

The code implements a nonogram solver, that is, it takes a list of block lengths and a total length and outputs all possible solutions to the nonogram puzzle. A nonogram is a logic puzzle with a grid of squares. Each row and column is assigned a set of numbers that indicate the lengths of unbroken lines of filled-in squares. The code uses a recursive function sequences to generate all possible sequences of filled-in squares that satisfy the given block lengths. The function takes two arguments: blockseq, a vector of block lengths, and numblanks, the total number of cells in the row or column. The function returns a vector of strings, each of which represents a possible sequence of filled-in squares. The function first checks if the blockseq is empty. If it is, the function returns a vector of strings containing a single string of the form "." ^ numblanks, which represents a row or column with no filled-in squares. If the blockseq is not empty, the function checks if the minlen of the blockseq is equal to the numblanks. The minlen of a blockseq is the sum of the block lengths plus the number of blocks minus 1. If the minlen is equal to the numblanks, the function returns a vector of strings containing a single string of the form join(map(x->"#"^x, blockseq), "."), which represents a row or column with the given block lengths. If the minlen is not equal to the numblanks, the function calls itself recursively with the allbuthead of the blockseq and the rightspace, which is the numblanks minus the minlen of the blockseq. The function returns a vector of strings containing all possible sequences of filled-in squares that satisfy the given block lengths and total length. The function nonoblocks takes two arguments: bvec, a vector of block lengths, and len, the total length. The function first prints a message to the console indicating the block lengths and the total length. If the len is less than the minlen of the bvec, the function prints a message to the console indicating that there is no solution. Otherwise, the function calls the sequences function with the bvec and len as arguments and prints each of the resulting sequences to the console.

Here is an example of how to use the nonoblocks function:

julia> nonoblocks([2, 1], 5)
With blocks [2, 1] and 5 cells:
.##...
##...

This example shows that there are two possible solutions to the nonogram puzzle with block lengths [2, 1] and total length 5.

Source code in the julia programming language

minsized(arr) = join(map(x->"#"^x, arr), ".")
minlen(arr) = sum(arr) + length(arr) - 1
 
function sequences(blockseq, numblanks)
    if isempty(blockseq)
        return ["." ^ numblanks]
    elseif minlen(blockseq) == numblanks
        return minsized(blockseq)
    else    
        result = Vector{String}()
        allbuthead = blockseq[2:end]
        for leftspace in 0:(numblanks - minlen(blockseq))
            header = "." ^ leftspace * "#" ^ blockseq[1] * "."
            rightspace = numblanks - length(header)
            if isempty(allbuthead)
                push!(result, rightspace <= 0 ? header[1:numblanks] : header * "." ^ rightspace)            
            elseif minlen(allbuthead) == rightspace
                push!(result, header * minsized(allbuthead))
            else
                map(x -> push!(result, header * x), sequences(allbuthead, rightspace))
            end
        end
    end
    result
end

function nonoblocks(bvec, len)
    println("With blocks $bvec and $len cells:")
    len < minlen(bvec) ? println("No solution") : for seq in sequences(bvec, len) println(seq) end
end

nonoblocks([2, 1], 5)
nonoblocks(Vector{Int}([]), 5)
nonoblocks([8], 10)
nonoblocks([2, 3, 2, 3], 15)
nonoblocks([2, 3], 5)


  

You may also check:How to resolve the algorithm Constrained random points on a circle step by step in the Prolog programming language
You may also check:How to resolve the algorithm Jacobsthal numbers step by step in the C++ programming language
You may also check:How to resolve the algorithm Arithmetic derivative step by step in the Raku programming language
You may also check:How to resolve the algorithm Latin Squares in reduced form step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Run-length encoding step by step in the PowerShell programming language