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

Published on 12 May 2024 09:40 PM

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

Source code in the echolisp programming language

;; size is the remaining # of cells
;; blocks is the list of remaining blocks size
;; cells is a stack where we push 0 = space or block size.
(define (nonoblock size blocks into: cells)
(cond
	((and (empty? blocks) (= 0 size)) (print-cells (stack->list cells)))
	
	((<= size 0) #f) ;; no hope - cut search
	((> (apply + blocks) size)  #f) ;; no hope - cut search
	
	(else
		(push cells 0) ;; space
		(nonoblock (1- size) blocks  cells)
		(pop cells)

	(when (!empty? blocks) 
		(when (stack-empty? cells) ;; first one (no space is allowed)
		(push cells (first blocks))
		(nonoblock  (- size (first blocks)) (rest blocks) cells)
		(pop cells))

		(push cells 0) ;; add space before
		(push cells (first blocks))
		(nonoblock  (- size (first blocks) 1) (rest blocks) cells)
		(pop cells)
		(pop cells)))))
	
(string-delimiter "")
(define block-symbs #( ?  📦 💣 💊  🍒 🌽 📘 📙 💰 🍯 ))

(define (print-cells cells)
	(writeln (string-append "|"
		(for/string ((cell cells)) 
			(if (zero? cell) "_"  
				(for/string ((i cell)) [block-symbs cell]))) "|")))
	
(define (task  nonotest)
	(for ((test nonotest)) 
		(define size (first test))
		(define blocks (second test))
		(printf "\n size:%d blocks:%d" size blocks)
		(if
			(> (+ (apply + blocks)(1- (length blocks))) size) 
				(writeln "❌ no solution for" size blocks)
			    (nonoblock size blocks (stack 'cells)))))


  

You may also check:How to resolve the algorithm Canonicalize CIDR step by step in the Python programming language
You may also check:How to resolve the algorithm Boolean values step by step in the Objeck programming language
You may also check:How to resolve the algorithm Stair-climbing puzzle step by step in the Aime programming language
You may also check:How to resolve the algorithm Bulls and cows step by step in the Vedit macro language programming language
You may also check:How to resolve the algorithm Almost prime step by step in the Wren programming language