Solve a Hidato puzzle: Difference between revisions
Content added Content deleted
m (→{{header|Tailspin}}: update to stricter typing) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 35: | Line 35: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="11l">[[Int]] board |
||
[Int] given |
[Int] given |
||
V start = (-1, -1) |
V start = (-1, -1) |
||
Line 102: | Line 102: | ||
solve(start[0], start[1], 1) |
solve(start[0], start[1], 1) |
||
print() |
print() |
||
print_board()</ |
print_board()</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 126: | Line 126: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
< |
<syntaxhighlight lang="autohotkey">SolveHidato(Grid, Locked, Max, row, col, num:=1, R:="", C:=""){ |
||
if (R&&C) ; if neighbors (not first iteration) |
if (R&&C) ; if neighbors (not first iteration) |
||
{ |
{ |
||
Line 192: | Line 192: | ||
} |
} |
||
return StrReplace(map, ">") |
return StrReplace(map, ">") |
||
}</ |
}</syntaxhighlight> |
||
Examples:< |
Examples:<syntaxhighlight lang="autohotkey">;-------------------------------- |
||
Grid := [[ "Y" , 33 , 35 , "Y" , "Y"] |
Grid := [[ "Y" , 33 , 35 , "Y" , "Y"] |
||
,[ "Y" , "Y" , 24 , 22 , "Y"] |
,[ "Y" , "Y" , 24 , 22 , "Y"] |
||
Line 216: | Line 216: | ||
;-------------------------------- |
;-------------------------------- |
||
MsgBox, 262144, ,% SolveHidato(Grid, Locked, Max, row, col) |
MsgBox, 262144, ,% SolveHidato(Grid, Locked, Max, row, col) |
||
return</ |
return</syntaxhighlight> |
||
Outputs:<pre>32 33 35 36 37 |
Outputs:<pre>32 33 35 36 37 |
||
31 34 24 22 38 |
31 34 24 22 38 |
||
Line 228: | Line 228: | ||
=={{header|Bracmat}}== |
=={{header|Bracmat}}== |
||
< |
<syntaxhighlight lang="bracmat">( |
||
( hidato |
( hidato |
||
= Line solve lowest Ncells row column rpad |
= Line solve lowest Ncells row column rpad |
||
Line 340: | Line 340: | ||
: ?board |
: ?board |
||
& out$(hidato$!board) |
& out$(hidato$!board) |
||
);</ |
);</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 364: | Line 364: | ||
=={{header|C}}== |
=={{header|C}}== |
||
Depth-first graph, with simple connectivity check to reject some impossible situations early. The checks slow down simpler puzzles significantly, but can make some deep recursions backtrack much earilier. |
Depth-first graph, with simple connectivity check to reject some impossible situations early. The checks slow down simpler puzzles significantly, but can make some deep recursions backtrack much earilier. |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
#include <string.h> |
#include <string.h> |
||
Line 530: | Line 530: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> Before: |
<pre> Before: |
||
Line 558: | Line 558: | ||
If there are cells that require more characters, then a 2-dimensional array of ints must be used. Any number < 0 indicates a no-go.<br/> |
If there are cells that require more characters, then a 2-dimensional array of ints must be used. Any number < 0 indicates a no-go.<br/> |
||
The puzzle can be made circular (the end cell must connect to the start cell). In that case, no start cell needs to be given. |
The puzzle can be made circular (the end cell must connect to the start cell). In that case, no start cell needs to be given. |
||
< |
<syntaxhighlight lang="csharp">using System.Collections; |
||
using System.Collections.Generic; |
using System.Collections.Generic; |
||
using static System.Console; |
using static System.Console; |
||
Line 685: | Line 685: | ||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 699: | Line 699: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
< |
<syntaxhighlight lang="cpp"> |
||
#include <iostream> |
#include <iostream> |
||
#include <sstream> |
#include <sstream> |
||
Line 852: | Line 852: | ||
} |
} |
||
//-------------------------------------------------------------------------------------------------- |
//-------------------------------------------------------------------------------------------------- |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 878: | Line 878: | ||
{{Works with|PAKCS}} |
{{Works with|PAKCS}} |
||
Probably not efficient. |
Probably not efficient. |
||
< |
<syntaxhighlight lang="curry">import CLPFD |
||
import Constraint (andC, anyC) |
import Constraint (andC, anyC) |
||
import Findall (unpack) |
import Findall (unpack) |
||
Line 931: | Line 931: | ||
] = success |
] = success |
||
main = unpack hidato</ |
main = unpack hidato</syntaxhighlight> |
||
{{Output}} |
{{Output}} |
||
<pre>Execution time: 1440 msec. / elapsed: 2270 msec. |
<pre>Execution time: 1440 msec. / elapsed: 2270 msec. |
||
Line 941: | Line 941: | ||
This version retains some of the characteristics of the original C version. It uses global variables, it doesn't enforce immutability and purity. This style is faster to write for prototypes, short programs or less important code, but in larger programs you usually want more strictness to avoid some bugs and increase long-term maintainability. |
This version retains some of the characteristics of the original C version. It uses global variables, it doesn't enforce immutability and purity. This style is faster to write for prototypes, short programs or less important code, but in larger programs you usually want more strictness to avoid some bugs and increase long-term maintainability. |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang="d">import std.stdio, std.array, std.conv, std.algorithm, std.string; |
||
int[][] board; |
int[][] board; |
||
Line 1,021: | Line 1,021: | ||
solve(start[0], start[1], 1); |
solve(start[0], start[1], 1); |
||
printBoard; |
printBoard; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> . . . . . . . . . . |
<pre> . . . . . . . . . . |
||
Line 1,051: | Line 1,051: | ||
With this coding style the changes in the code become less bug-prone, but also more laborious. This version is also faster, its total runtime is about 0.02 seconds or less. |
With this coding style the changes in the code become less bug-prone, but also more laborious. This version is also faster, its total runtime is about 0.02 seconds or less. |
||
< |
<syntaxhighlight lang="d">import std.stdio, std.conv, std.ascii, std.array, std.string, |
||
std.algorithm, std.exception, std.range, std.typetuple; |
std.algorithm, std.exception, std.range, std.typetuple; |
||
Line 1,265: | Line 1,265: | ||
. . . _ _ . . _ _ . . _ _ . . _ _ . . _ _ . . _ _ . . _ _ . . _ _ . . _ _ . . _ _ . . _ _ . . _ _ ." |
. . . _ _ . . _ _ . . _ _ . . _ _ . . _ _ . . _ _ . . _ _ . . _ _ . . _ _ . . _ _ . . _ _ . . _ _ ." |
||
); |
); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Problem: |
<pre>Problem: |
||
Line 1,309: | Line 1,309: | ||
=={{header|Elixir}}== |
=={{header|Elixir}}== |
||
{{trans|Ruby}} |
{{trans|Ruby}} |
||
< |
<syntaxhighlight lang="elixir"># Solve a Hidato Like Puzzle with Warnsdorff like logic applied |
||
# |
# |
||
defmodule HLPsolver do |
defmodule HLPsolver do |
||
Line 1,386: | Line 1,386: | ||
end) |
end) |
||
end |
end |
||
end</ |
end</syntaxhighlight> |
||
'''Test:''' |
'''Test:''' |
||
< |
<syntaxhighlight lang="elixir">adjacent = [{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}] |
||
""" |
""" |
||
Line 1,415: | Line 1,415: | ||
. . 0 0 0 . 0 0 0 . 0 0 0 . 0 0 0 . 0 0 0 . 0 0 0 . 0 |
. . 0 0 0 . 0 0 0 . 0 0 0 . 0 0 0 . 0 0 0 . 0 0 0 . 0 |
||
""" |
""" |
||
|> HLPsolver.solve(adjacent)</ |
|> HLPsolver.solve(adjacent)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,462: | Line 1,462: | ||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
To simplify the code I start a new process for searching each potential path through the grid. This means that the default maximum number of processes had to be raised ("erl +P 50000" works for me). The task takes about 1-2 seconds on a low level Mac mini. If faster times are needed, or even less performing hardware is used, some optimisation should be done. |
To simplify the code I start a new process for searching each potential path through the grid. This means that the default maximum number of processes had to be raised ("erl +P 50000" works for me). The task takes about 1-2 seconds on a low level Mac mini. If faster times are needed, or even less performing hardware is used, some optimisation should be done. |
||
<syntaxhighlight lang="erlang"> |
|||
<lang Erlang> |
|||
-module( solve_hidato_puzzle ). |
-module( solve_hidato_puzzle ). |
||
Line 1,567: | Line 1,567: | ||
store( {Key, Value}, Dict ) -> dict:store( Key, Value, Dict ). |
store( {Key, Value}, Dict ) -> dict:store( Key, Value, Dict ). |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,595: | Line 1,595: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 1,712: | Line 1,712: | ||
solve(start[0], start[1], 1, 0) |
solve(start[0], start[1], 1, 0) |
||
printBoard() |
printBoard() |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,741: | Line 1,741: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">{-# LANGUAGE TupleSections #-} |
||
{-# LANGUAGE Rank2Types #-} |
{-# LANGUAGE Rank2Types #-} |
||
Line 1,851: | Line 1,851: | ||
, ". . . . 0 7 0 0" |
, ". . . . 0 7 0 0" |
||
, ". . . . . . 5 0" |
, ". . . . . . 5 0" |
||
]</ |
]</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre> 0 33 35 0 0 |
<pre> 0 33 35 0 0 |
||
Line 1,875: | Line 1,875: | ||
This is an Unicon-specific solution but could easily be adjusted to work in Icon. |
This is an Unicon-specific solution but could easily be adjusted to work in Icon. |
||
< |
<syntaxhighlight lang="unicon">global nCells, cMap, best |
||
record Pos(r,c) |
record Pos(r,c) |
||
Line 1,979: | Line 1,979: | ||
QMouse(puzzle, goWest(), self, val) |
QMouse(puzzle, goWest(), self, val) |
||
QMouse(puzzle, goNW(), self, val) |
QMouse(puzzle, goNW(), self, val) |
||
end</ |
end</syntaxhighlight> |
||
Sample run: |
Sample run: |
||
Line 2,013: | Line 2,013: | ||
{{trans|D}} |
{{trans|D}} |
||
{{works with|Java|7}} |
{{works with|Java|7}} |
||
< |
<syntaxhighlight lang="java">import java.util.ArrayList; |
||
import java.util.Collections; |
import java.util.Collections; |
||
import java.util.List; |
import java.util.List; |
||
Line 2,117: | Line 2,117: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Output: |
Output: |
||
Line 2,146: | Line 2,146: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
This solution utilizes a Hidato puzzle solver module which is also used for the Hopido and knight move tasks. |
This solution utilizes a Hidato puzzle solver module which is also used for the Hopido and knight move tasks. |
||
< |
<syntaxhighlight lang="julia">module Hidato |
||
export hidatosolve, printboard, hidatoconfigure |
export hidatosolve, printboard, hidatoconfigure |
||
Line 2,202: | Line 2,202: | ||
end # module |
end # module |
||
</ |
</syntaxhighlight><syntaxhighlight lang="julia">using .Hidato |
||
hidat = """ |
hidat = """ |
||
Line 2,220: | Line 2,220: | ||
hidatosolve(board, maxmoves, kingmoves, fixed, starts[1][1], starts[1][2], 1) |
hidatosolve(board, maxmoves, kingmoves, fixed, starts[1][1], starts[1][2], 1) |
||
printboard(board) |
printboard(board) |
||
</ |
</syntaxhighlight>{{output}}<pre> |
||
__ 33 35 __ __ |
__ 33 35 __ __ |
||
__ __ 24 22 __ |
__ __ 24 22 __ |
||
Line 2,242: | Line 2,242: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="scala">// version 1.2.0 |
||
lateinit var board: List<IntArray> |
lateinit var board: List<IntArray> |
||
Line 2,316: | Line 2,316: | ||
solve(start[0], start[1], 1, 0) |
solve(start[0], start[1], 1, 0) |
||
printBoard() |
printBoard() |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,345: | Line 2,345: | ||
=={{header|Mathprog}}== |
=={{header|Mathprog}}== |
||
< |
<syntaxhighlight lang="mathprog">/*Hidato.mathprog, part of KuKu by Nigel Galloway |
||
Find a solution to a Hidato problem |
Find a solution to a Hidato problem |
||
Line 2,408: | Line 2,408: | ||
; |
; |
||
end;</ |
end;</syntaxhighlight> |
||
Using the data in the model produces the following: |
Using the data in the model produces the following: |
||
{{out}} |
{{out}} |
||
Line 2,654: | Line 2,654: | ||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
This implements a solver that works based on various techniques, i.e. not brute-forcing: |
This implements a solver that works based on various techniques, i.e. not brute-forcing: |
||
< |
<syntaxhighlight lang="mathematica">ClearAll[NeighbourQ, CellDistance, VisualizeHidato, HiddenSingle, \ |
||
NakedN, HiddenN, ChainSearch, HidatoSolve, Cornering, ValidPuzzle, \ |
NakedN, HiddenN, ChainSearch, HidatoSolve, Cornering, ValidPuzzle, \ |
||
GapSearch, ReachDelete, GrowNeighbours] |
GapSearch, ReachDelete, GrowNeighbours] |
||
Line 3,000: | Line 3,000: | ||
VisualizeHidato[cells, candidates] |
VisualizeHidato[cells, candidates] |
||
out = HidatoSolve[cells, candidates]; |
out = HidatoSolve[cells, candidates]; |
||
VisualizeHidato[cells, out]</ |
VisualizeHidato[cells, out]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Outputs a graphical version of the solved hidato. |
Outputs a graphical version of the solved hidato. |
||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang="nim">import strutils, algorithm, sequtils, strformat |
||
type Hidato = object |
type Hidato = object |
||
Line 3,083: | Line 3,083: | ||
echo("Found:") |
echo("Found:") |
||
discard hidato.solve(hidato.start[0], hidato.start[1], 1) |
discard hidato.solve(hidato.start[0], hidato.start[1], 1) |
||
hidato.print()</ |
hidato.print()</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> . . . . . . . . . . |
<pre> . . . . . . . . . . |
||
Line 3,109: | Line 3,109: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang="perl">use strict; |
||
use List::Util 'max'; |
use List::Util 'max'; |
||
Line 3,192: | Line 3,192: | ||
print "\033[2J"; |
print "\033[2J"; |
||
try_fill(1, $known[1]);</ |
try_fill(1, $known[1]);</syntaxhighlight>{{out}} |
||
32 33 35 36 37 |
32 33 35 36 37 |
||
31 34 24 22 38 |
31 34 24 22 38 |
||
Line 3,203: | Line 3,203: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">board</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">warnsdorffs</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">knownx</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">knowny</span> |
<span style="color: #004080;">sequence</span> <span style="color: #000000;">board</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">warnsdorffs</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">knownx</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">knowny</span> |
||
Line 3,379: | Line 3,379: | ||
__ __ __ .. .. __ __ __ .. .. __ __ __ .. .. __ __ __ .. .. __ __ __ .. .. __ __ __ .. .. __ __ __ .. .. __ __ __ .. .. __ __ __ .. .. .."""</span> |
__ __ __ .. .. __ __ __ .. .. __ __ __ .. .. __ __ __ .. .. __ __ __ .. .. __ __ __ .. .. __ __ __ .. .. __ __ __ .. .. __ __ __ .. .. .."""</span> |
||
<span style="color: #000000;">Hidato</span><span style="color: #0000FF;">(</span><span style="color: #000000;">board6</span><span style="color: #0000FF;">,</span><span style="color: #000000;">46</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">82</span><span style="color: #0000FF;">)</span> |
<span style="color: #000000;">Hidato</span><span style="color: #0000FF;">(</span><span style="color: #000000;">board6</span><span style="color: #0000FF;">,</span><span style="color: #000000;">46</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">82</span><span style="color: #0000FF;">)</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre style="font-size: 8px"> |
<pre style="font-size: 8px"> |
||
Line 3,427: | Line 3,427: | ||
=={{header|Picat}}== |
=={{header|Picat}}== |
||
< |
<syntaxhighlight lang="picat">import sat. |
||
main => |
main => |
||
Line 3,472: | Line 3,472: | ||
R1 >= 1, R1 =< MaxR, C1 >= 1, C1 =< MaxC, |
R1 >= 1, R1 =< MaxR, C1 >= 1, C1 =< MaxC, |
||
(R1,C1) != (R,C), M[R1,C1] !== 0]. |
(R1,C1) != (R,C), M[R1,C1] !== 0]. |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,486: | Line 3,486: | ||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
< |
<syntaxhighlight lang="picolisp">(load "@lib/simul.l") |
||
(de hidato (Lst) |
(de hidato (Lst) |
||
Line 3,530: | Line 3,530: | ||
(disp Grid 0 |
(disp Grid 0 |
||
'((This) |
'((This) |
||
(if (: val) (align 3 @) " ") ) ) ) )</ |
(if (: val) (align 3 @) " ") ) ) ) )</syntaxhighlight> |
||
Test: |
Test: |
||
< |
<syntaxhighlight lang="picolisp">(hidato |
||
(quote |
(quote |
||
(T 33 35 T T) |
(T 33 35 T T) |
||
Line 3,541: | Line 3,541: | ||
(NIL NIL T T 18 T T) |
(NIL NIL T T 18 T T) |
||
(NIL NIL NIL NIL T 7 T T) |
(NIL NIL NIL NIL T 7 T T) |
||
(NIL NIL NIL NIL NIL NIL 5 T) ) )</ |
(NIL NIL NIL NIL NIL NIL 5 T) ) )</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> +---+---+---+---+---+---+---+---+ |
<pre> +---+---+---+---+---+---+---+---+ |
||
Line 3,565: | Line 3,565: | ||
Works with SWI-Prolog and library(clpfd) written by '''Markus Triska'''.<br> |
Works with SWI-Prolog and library(clpfd) written by '''Markus Triska'''.<br> |
||
Puzzle solved is from the Wilkipedia page : http://en.wikipedia.org/wiki/Hidato |
Puzzle solved is from the Wilkipedia page : http://en.wikipedia.org/wiki/Hidato |
||
< |
<syntaxhighlight lang="prolog">:- use_module(library(clpfd)). |
||
hidato :- |
hidato :- |
||
Line 3,664: | Line 3,664: | ||
my_write_1(X) :- |
my_write_1(X) :- |
||
writef('%3r', [X]).</ |
writef('%3r', [X]).</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>?- hidato. |
<pre>?- hidato. |
||
Line 3,678: | Line 3,678: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
< |
<syntaxhighlight lang="python">board = [] |
||
given = [] |
given = [] |
||
start = None |
start = None |
||
Line 3,746: | Line 3,746: | ||
solve(start[0], start[1], 1) |
solve(start[0], start[1], 1) |
||
print |
print |
||
print_board()</ |
print_board()</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> __ 33 35 __ __ |
<pre> __ 33 35 __ __ |
||
Line 3,772: | Line 3,772: | ||
immediately when tested with custom 2d vector library. |
immediately when tested with custom 2d vector library. |
||
<syntaxhighlight lang="racket"> |
|||
<lang Racket> |
|||
#lang racket |
#lang racket |
||
(require math/array) |
(require math/array) |
||
Line 3,842: | Line 3,842: | ||
;main function |
;main function |
||
(define (hidato board) (put-path board (hidato-path board))) |
(define (hidato board) (put-path board (hidato-path board))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,863: | Line 3,863: | ||
essentially the neighbourhood function. |
essentially the neighbourhood function. |
||
< |
<syntaxhighlight lang="racket">#lang racket |
||
(require "hidato-family-solver.rkt") |
(require "hidato-family-solver.rkt") |
||
Line 3,882: | Line 3,882: | ||
#( _ _ _ _ 0 7 0 0) |
#( _ _ _ _ 0 7 0 0) |
||
#( _ _ _ _ _ _ 5 0))))) |
#( _ _ _ _ _ _ 5 0))))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 3,905: | Line 3,905: | ||
* [[Solve the no connection puzzle#Raku|Solve the no connection puzzle]] |
* [[Solve the no connection puzzle#Raku|Solve the no connection puzzle]] |
||
<lang |
<syntaxhighlight lang="raku" line>my @adjacent = [-1, -1], [-1, 0], [-1, 1], |
||
[ 0, -1], [ 0, 1], |
[ 0, -1], [ 0, 1], |
||
[ 1, -1], [ 1, 0], [ 1, 1]; |
[ 1, -1], [ 1, 0], [ 1, 1]; |
||
Line 4,002: | Line 4,002: | ||
say "$tries tries"; |
say "$tries tries"; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
Line 4,011: | Line 4,011: | ||
''Hidato'' and ''Numbrix'' are registered trademarks. |
''Hidato'' and ''Numbrix'' are registered trademarks. |
||
< |
<syntaxhighlight lang="rexx">/*REXX program solves a Numbrix (R) puzzle, it also displays the puzzle and solution. */ |
||
maxR=0; maxC=0; maxX=0; minR=9e9; minC=9e9; minX=9e9; cells=0; @.= |
maxR=0; maxC=0; maxX=0; minR=9e9; minC=9e9; minX=9e9; cells=0; @.= |
||
parse arg xxx; PZ='Hidato puzzle' /*get the cell definitions from the CL.*/ |
parse arg xxx; PZ='Hidato puzzle' /*get the cell definitions from the CL.*/ |
||
Line 4,065: | Line 4,065: | ||
say _ |
say _ |
||
end /*r*/ |
end /*r*/ |
||
say; return</ |
say; return</syntaxhighlight> |
||
'''output''' when using the following as input: |
'''output''' when using the following as input: |
||
<br> <tt> 1 7 5 .\2 5 . 7 . .\3 3 . . 18 . .\4 1 27 . . . 9 . 1\5 1 . 26 . 13 40 11\6 1 . . . 21 . .\7 1 . . 24 22 .\8 1 . 33 35 . .</tt> |
<br> <tt> 1 7 5 .\2 5 . 7 . .\3 3 . . 18 . .\4 1 27 . . . 9 . 1\5 1 . 26 . 13 40 11\6 1 . . . 21 . .\7 1 . . 24 22 .\8 1 . 33 35 . .</tt> |
||
Line 4,094: | Line 4,094: | ||
===Without Warnsdorff=== |
===Without Warnsdorff=== |
||
The following class provides functionality for solving a hidato problem: |
The following class provides functionality for solving a hidato problem: |
||
< |
<syntaxhighlight lang="ruby"># Solve a Hidato Puzzle |
||
# |
# |
||
class Hidato |
class Hidato |
||
Line 4,148: | Line 4,148: | ||
(msg ? [msg] : []) + str + [""] |
(msg ? [msg] : []) + str + [""] |
||
end |
end |
||
end</ |
end</syntaxhighlight> |
||
'''Test:''' |
'''Test:''' |
||
< |
<syntaxhighlight lang="ruby"># Which may be used as follows to solve Evil Case 1: |
||
board1 = <<EOS |
board1 = <<EOS |
||
. 4 |
. 4 |
||
Line 4,180: | Line 4,180: | ||
t0 = Time.now |
t0 = Time.now |
||
Hidato.new(board3).solve |
Hidato.new(board3).solve |
||
puts " #{Time.now - t0} sec"</ |
puts " #{Time.now - t0} sec"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,229: | Line 4,229: | ||
===With Warnsdorff=== |
===With Warnsdorff=== |
||
I modify method as follows to implement [[wp:Knight's_tour#Warnsdorff|Warnsdorff]] like |
I modify method as follows to implement [[wp:Knight's_tour#Warnsdorff|Warnsdorff]] like |
||
< |
<syntaxhighlight lang="ruby"># Solve a Hidato Like Puzzle with Warnsdorff like logic applied |
||
# |
# |
||
class HLPsolver |
class HLPsolver |
||
Line 4,296: | Line 4,296: | ||
(msg ? [msg] : []) + str + [""] |
(msg ? [msg] : []) + str + [""] |
||
end |
end |
||
end</ |
end</syntaxhighlight> |
||
Which may be used as follows to solve Hidato Puzzles: |
Which may be used as follows to solve Hidato Puzzles: |
||
< |
<syntaxhighlight lang="ruby">require 'HLPsolver' |
||
ADJACENT = [[-1, -1], [-1, 0], [-1, 1], [0, -1], [0, 1], [1, -1], [1, 0], [1, 1]] |
ADJACENT = [[-1, -1], [-1, 0], [-1, 1], [0, -1], [0, 1], [1, -1], [1, 0], [1, 1]] |
||
Line 4,343: | Line 4,343: | ||
t0 = Time.now |
t0 = Time.now |
||
HLPsolver.new(board3).solve |
HLPsolver.new(board3).solve |
||
puts " #{Time.now - t0} sec"</ |
puts " #{Time.now - t0} sec"</syntaxhighlight> |
||
Which produces: |
Which produces: |
||
Line 4,413: | Line 4,413: | ||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
< |
<syntaxhighlight lang="rust"> |
||
use std::cmp::{max, min}; |
use std::cmp::{max, min}; |
||
use std::fmt; |
use std::fmt; |
||
Line 4,608: | Line 4,608: | ||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 4,631: | Line 4,631: | ||
</pre> |
</pre> |
||
=={{header|Seed7}}== |
=={{header|Seed7}}== |
||
< |
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i"; |
||
var set of integer: given is {}; |
var set of integer: given is {}; |
||
Line 4,730: | Line 4,730: | ||
printBoard; |
printBoard; |
||
end if; |
end if; |
||
end func;</ |
end func;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,760: | Line 4,760: | ||
=={{header|Tailspin}}== |
=={{header|Tailspin}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="tailspin"> |
||
def input: |
def input: |
||
'__ 33 35 __ __ . . . |
'__ 33 35 __ __ . . . |
||
Line 4,807: | Line 4,807: | ||
'; |
'; |
||
' ->!OUT::write |
' ->!OUT::write |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 4,821: | Line 4,821: | ||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
< |
<syntaxhighlight lang="tcl">proc init {initialConfiguration} { |
||
global grid max filled |
global grid max filled |
||
set max 1 |
set max 1 |
||
Line 4,930: | Line 4,930: | ||
puts "" |
puts "" |
||
printgrid |
printgrid |
||
}</ |
}</syntaxhighlight> |
||
Demonstrating (dots are “outside” the grid, and zeroes are the cells to be filled in): |
Demonstrating (dots are “outside” the grid, and zeroes are the cells to be filled in): |
||
< |
<syntaxhighlight lang="tcl">solveHidato " |
||
0 33 35 0 0 . . . |
0 33 35 0 0 . . . |
||
0 0 24 22 0 . . . |
0 0 24 22 0 . . . |
||
Line 4,941: | Line 4,941: | ||
. . . . 0 7 0 0 |
. . . . 0 7 0 0 |
||
. . . . . . 5 0 |
. . . . . . 5 0 |
||
"</ |
"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 4,972: | Line 4,972: | ||
{{libheader|Wren-sort}} |
{{libheader|Wren-sort}} |
||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
< |
<syntaxhighlight lang="ecmascript">import "/sort" for Sort |
||
import "/fmt" for Fmt |
import "/fmt" for Fmt |
||
Line 5,050: | Line 5,050: | ||
System.print("\nFound:") |
System.print("\nFound:") |
||
solve.call(start[0], start[1], 1, 0) |
solve.call(start[0], start[1], 1, 0) |
||
printBoard.call()</ |
printBoard.call()</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 5,080: | Line 5,080: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="zkl">hi:= // 0==empty cell, X==not a cell |
||
#<<< |
#<<< |
||
"0 33 35 0 0 X X X |
"0 33 35 0 0 X X X |
||
Line 5,096: | Line 5,096: | ||
solve(board,given, start.xplode(), 1); |
solve(board,given, start.xplode(), 1); |
||
println(); |
println(); |
||
print_board(board);</ |
print_board(board);</syntaxhighlight> |
||
< |
<syntaxhighlight lang="zkl">fcn print_board(board){ |
||
d:=D(-1," ", 0,"__"); |
d:=D(-1," ", 0,"__"); |
||
foreach r in (board[1,-1]){ |
foreach r in (board[1,-1]){ |
||
Line 5,133: | Line 5,133: | ||
board[r][c]=back; |
board[r][c]=back; |
||
False |
False |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |