How to resolve the algorithm Solve a Hidato puzzle step by step in the Tailspin programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Solve a Hidato puzzle step by step in the Tailspin 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 Tailspin programming language
Source code in the tailspin programming language
def input:
'__ 33 35 __ __ . . .
__ __ 24 22 __ . . .
__ __ __ 21 __ __ . .
__ 26 __ 13 40 11 . .
27 __ __ __ 9 __ 1 .
. . __ __ 18 __ __ .
. . . . __ 7 __ __
. . . . . . 5 __';
templates hidato
composer setup
data givenInput , col: }>*]> local
@: {row: 1, col: 1, givenInput:n´1:[]};
{ board: row´1:[ + ], given: $@.givenInput -> \[i](<~´{}´ ={}> { n: $i, $...} !\) }
rule line: col´1:[ + ] (<'\n '>?) (..|@: {row: $@.row::raw + 1, col: 1};) |
rule cell: (<' '>?) (@.col: $@.col::raw + 1;)
rule open: <'__'> -> n´0
rule blocked: <' \.'> -> n´-1
rule given: (<' '>?) (def given: ;)
($given -> ..|@.givenInput: $@.givenInput::length+1..$::raw -> {};)
($given -> @.givenInput($): { row: $@.row, col: $@.col };)
$given
end setup
templates solve
when <~{row: <1..$@hidato.board::length>, col: <1..$@hidato.board(row´1)::length>}> do !VOID
when <{ n: <=$@hidato.given(last).n>, row: <=$@hidato.given(last).row>, col: <=$@hidato.given(last).col> }> do $@hidato.board !
when ($@hidato.board($.row; $.col) <~=n´0|=$.n>)> do !VOID
when ($@hidato.board($.row; $.col) <=n´0>)?($@hidato.given($.next) <{n: <=$.n>}>)> do !VOID
otherwise
def guess: $;
def back: $@hidato.board($.row; $.col);
def next: $ -> \(when <{n: <=$back>}> do n´($.next::raw + 1)! otherwise $.next!\);
@hidato.board($.row; $.col): $.n;
0..8 -> { next: $next, n: $guess.n::raw + 1, row: $guess.row::raw + $ ~/ 3 - 1, col: $guess.col::raw + $ mod 3 - 1 } -> #
@hidato.board($.row; $.col): $back;
end solve
@: $ -> setup;
{ next: n´1, $@.given(first)... } -> solve !
end hidato
$input -> hidato -> '$... -> '$... -> ' $ -> \(when <=n´-1> do ' .' ! when do '$;' ! otherwise ' $;' !\);';
';
' ->!OUT::write
You may also check:How to resolve the algorithm Last Friday of each month step by step in the Ada programming language
You may also check:How to resolve the algorithm Trigonometric functions step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Terminal control/Cursor positioning step by step in the jq programming language
You may also check:How to resolve the algorithm EKG sequence convergence step by step in the Haskell programming language
You may also check:How to resolve the algorithm Define a primitive data type step by step in the Go programming language