How to resolve the algorithm Solve a Hidato puzzle step by step in the Wren programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Solve a Hidato puzzle step by step in the Wren programming language

Table of Contents

Problem Statement

The task is to write a program which solves Hidato (aka Hidoku) puzzles. The rules are: For example the following problem has the following solution, with path marked on it:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Solve a Hidato puzzle step by step in the Wren programming language

Source code in the wren programming language

import "/sort" for Sort
import "/fmt" for Fmt

var board = []
var given = []
var start = []

var setUp = Fn.new { |input|
    var nRows = input.count
    var puzzle = List.filled(nRows, null)
    for (i in 0...nRows) puzzle[i] = input[i].split(" ")
    var nCols = puzzle[0].count
    var list = []
    board = List.filled(nRows+2, null)
    for (i in 0...board.count) board[i] = List.filled(nCols+2, -1)
    for (r in 0...nRows) {
        var row = puzzle[r]
        for (c in 0...nCols) {
            var cell = row[c]
            if (cell == "_") {
                board[r + 1][c + 1] = 0
            } else if (cell != ".") {
                var value = Num.fromString(cell)
                board[r + 1][c + 1] = value
                list.add(value)
                if (value == 1) start = [r + 1, c + 1]
            }
        }
    }
    Sort.quick(list)
    given = list
}

var solve // recursive
solve = Fn.new { |r, c, n, next|
    if (n > given[-1]) return true
    var back = board[r][c]
    if (back != 0 && back != n) return false
    if (back == 0 && given[next] == n) return false
    var next2 = next
    if (back == n) next2 = next2 + 1
    board[r][c] = n
    for (i in -1..1) {
        for (j in -1..1) if (solve.call(r + i, c + j, n + 1, next2)) return true
    }
    board[r][c] = back
    return false
}

var printBoard = Fn.new {
    for (row in board) {
        for (c in row) {
            if (c == -1) {
                System.write(" . ")
            } else if (c > 0) {
                Fmt.write("$2d ", c)
            } else {
                System.write("__ ")
            }
        }
        System.print()
    }
}

var input = [
    "_ 33 35 _ _ . . .",
    "_ _ 24 22 _ . . .",
    "_ _ _ 21 _ _ . .",
    "_ 26 _ 13 40 11 . .",
    "27 _ _ _ 9 _ 1 .",
    ". . _ _ 18 _ _ .",
    ". . . . _ 7 _ _",
    ". . . . . . 5 _"
]
setUp.call(input)
printBoard.call()
System.print("\nFound:")
solve.call(start[0], start[1], 1, 0)
printBoard.call()

  

You may also check:How to resolve the algorithm Apply a digital filter (direct form II transposed) step by step in the MATLAB programming language
You may also check:How to resolve the algorithm Collections step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Compound data type step by step in the Maxima programming language
You may also check:How to resolve the algorithm Arrays step by step in the Wee Basic programming language
You may also check:How to resolve the algorithm Singly-linked list/Element definition step by step in the Bracmat programming language