Solve a Hopido puzzle: Difference between revisions

m
m (→‎{{header|Wren}}: Minor tidy)
 
(3 intermediate revisions by 2 users not shown)
Line 32:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">V neighbours = [[2, 2], [-2, 2], [2, -2], [-2, -2], [3, 0], [0, 3], [-3, 0], [0, -3]]
V cnt = 0
V pWid = 0
Line 86:
print()
E
print(‘No solution!’, end' ‘’)</langsyntaxhighlight>
 
{{out}}
Line 99:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">SolveHopido(Grid, Locked, Max, row, col, num:=1, R:="", C:=""){
if (R&&C) ; if neighbors (not first iteration)
{
Line 168:
}
return StrReplace(map, ">")
}</langsyntaxhighlight>
Examples:<langsyntaxhighlight AutoHotkeylang="autohotkey">;--------------------------------
Grid := [["",0 ,0 ,"",0 ,0 ,""]
,[0 ,0 ,0 ,0 ,0 ,0 ,0]
Line 197:
;--------------------------------
MsgBox, 262144, ,% SolveHopido(Grid, Locked, Max, row, col)
return</langsyntaxhighlight>
Outputs:<pre> 17 24 16 25
22 8 11 21 7 10 20
Line 210:
Any non-numeric value 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/>
<langsyntaxhighlight lang="csharp">using System.Collections;
using System.Collections.Generic;
using static System.Console;
Line 335:
}
 
}</langsyntaxhighlight>
{{out}}
<pre style="height:30ex;overflow:scroll">
Line 347:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">
#include <vector>
#include <sstream>
Line 481:
return system( "pause" );
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 495:
{{trans|C++}}
From the refactored C++ version with more precise typing. This tries all possible start positions. The HopidoPuzzle struct is created at compile-time, so its pre-conditions can catch most malformed puzzles at compile-time.
<langsyntaxhighlight lang="d">import std.stdio, std.conv, std.string, std.range, std.algorithm, std.typecons;
 
 
Line 608:
else
writefln("One solution:\n%(%-(%2s %)\n%)", solution);
}</langsyntaxhighlight>
{{out}}
<pre>One solution:
Line 621:
{{trans|Ruby}}
This solution uses HLPsolver from [[Solve_a_Hidato_puzzle#Elixir | here]]
<langsyntaxhighlight lang="elixir"># require HLPsolver
adjacent = [{-3, 0}, {0, -3}, {0, 3}, {3, 0}, {-2, -2}, {-2, 2}, {2, -2}, {2, 2}]
Line 633:
. . . 1 . . .
"""
HLPsolver.solve(board, adjacent)</langsyntaxhighlight>
 
{{out}}
Line 656:
=={{header|Go}}==
{{trans|Java}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 778:
}
printResult()
}</langsyntaxhighlight>
 
{{out}}
Line 794:
Minor variant of [[Solve_a_Holy_Knight's_tour]]. Works in Unicon only.
 
<langsyntaxhighlight lang="unicon">global nCells, cMap, best
record Pos(r,c)
 
Line 891:
QMouse(puzzle, visit(loc.r, loc.c+3), self, val)
QMouse(puzzle, visit(loc.r-2,loc.c+2), self, val)
end</langsyntaxhighlight>
 
Sample run:
Line 923:
=={{header|Java}}==
{{works with|Java|8}}
<langsyntaxhighlight lang="java">import java.util.*;
 
public class Hopido {
Line 1,031:
}
}
}</langsyntaxhighlight>
 
<pre>
Line 1,043:
=={{header|Julia}}==
Uses the Hidato puzzle solver module, which has its source code listed [[Solve_a_Hidato_puzzle#Julia | here]] in the Hadato task.
<langsyntaxhighlight lang="julia">using .Hidato # Note that the . here means to look locally for the module rather than in the libraries
 
const hopid = """
Line 1,059:
hidatosolve(board, maxmoves, hopidomoves, fixed, starts[1][1], starts[1][2], 1)
printboard(board)
</langsyntaxhighlight>{{output}}<pre>
0 0 0 0
0 0 0 0 0 0 0
Line 1,077:
=={{header|Kotlin}}==
{{trans|Java}}
<langsyntaxhighlight lang="scala">// version 1.2.0
 
val board = listOf(
Line 1,172:
 
printResult()
}</langsyntaxhighlight>
 
{{out}}
Line 1,186:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Uses shortest tours on graphs to solve it:
<langsyntaxhighlight Mathematicalang="mathematica">puzz = ".00.00.\n0000000\n0000000\n.00000.\n..000..\n...0...";
puzz //= StringSplit[#, "\n"] & /* Map[Characters];
puzz //= Transpose /* Map[Reverse];
Line 1,193:
g = Graph[UndirectedEdge @@@ moves];
ord = Most[FindShortestTour[g][[2]]];
Graphics[MapThread[Text, {Range[Length[ord]], VertexList[g][[ord]]}]]</langsyntaxhighlight>
{{out}}
Shows a graphical solution.
Line 1,200:
{{trans|Go}}
 
<langsyntaxhighlight Nimlang="nim">import algorithm, sequtils, strformat
 
const Moves = [(-3, 0), (0, 3), ( 3, 0), ( 0, -3),
Line 1,294:
var hopido = initHopido(Board)
hopido.findSolution()
hopido.print()</langsyntaxhighlight>
 
{{out}}
Line 1,310:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; # http://www.rosettacode.org/wiki/Solve_a_Hopido_puzzle
Line 1,347:
. 0 0 0 0 0 .
. . 0 0 0 . .
. . . 0 . . .</langsyntaxhighlight>
{{out}}
<pre>
Line 1,362:
=={{header|Phix}}==
Simple brute force approach.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
Line 1,425:
. . . 0 . . ."""</span>
<span style="color: #000000;">Hopido</span><span style="color: #0000FF;">(</span><span style="color: #000000;">board1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
The best and worse cases observed were:
<pre>
Line 1,444:
</pre>
=={{header|Picat}}==
<syntaxhighlight lang="picat">
<lang Picat>
import sat.
main =>
Grid = {{0,1,1,0,1,1,0},
{1,1,1,1,1,1,1},
{1,1,1,1,1,1,1},
{0,1,1,1,1,1,0},
{0,0,1,1,1,0,0},
{0,0,0,1,0,0,0}},
NR = len(Grid), NC = len(Grid[1]),
Es = [{(R,C), (R1,C1), _} : R in 1..NR, C in 1..NC, R1 in 1..NR, C1 in 1..NC, % Edges
Line 1,472:
if M[R,C] = 0 then print(" ") else printf("%2d ", M[R,C]) end,
if C = NC then nl end
end. /*
</syntaxhighlight>
24 15 23 26
Output:
<pre> 24 15 23 26
6 9 12 5 8 11 4
14 17 20 25 16 19 22
Line 1,479 ⟶ 1,481:
13 18 21
1
CPU time 0.019 seconds, correct *</pre>
</lang>
 
=={{header|Prolog}}==
This is a pure prolog implementation (no cuts,etc..), the only libary predicate used is select/3 witch is pure.
<langsyntaxhighlight lang="prolog">hopido(Grid,[C|Solved],Xs,Ys) :-
select(C,Grid,RGrid),
solve(RGrid,C,Solved,Xs,Ys).
Line 1,508 ⟶ 1,509:
 
j3(O,N,[O,_,_,N|_]).
j3(O,N,[_|T]) :- j3(O,N,T).</langsyntaxhighlight>
To test send in a list of p/2 terms that represent points that can be hopped to (order is not important).
 
The grid coords can be anything so need to specify the valid coordinates as a list (in this case numbers between 0 and 6).
<langsyntaxhighlight lang="prolog">puzzle([
p(1,0),p(2,0) ,p(4,0),p(5,0),
p(0,1),p(1,1),p(2,1),p(3,1),p(4,1),p(5,1),p(6,1),
Line 1,525 ⟶ 1,526:
XYs = [0,1,2,3,4,5,6],
hopido(P,S,XYs,XYs),
maplist(writeln,S).</langsyntaxhighlight>
{{out}}
<pre>
Line 1,561 ⟶ 1,562:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">
from sys import stdout
 
Line 1,625 ⟶ 1,626:
else:
stdout.write("No solution!")
</langsyntaxhighlight> {{out}}<pre>
01 25 17 03
27 13 10 07 14 11 08
Line 1,640 ⟶ 1,641:
essentially the neighbourhood function.
 
<langsyntaxhighlight lang="racket">#lang racket
(require "hidato-family-solver.rkt")
 
Line 1,657 ⟶ 1,658:
#(_ _ 0 0 0 _ _)
#(_ _ _ 0 _ _ _)))))
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,678 ⟶ 1,679:
* [[Solve the no connection puzzle#Raku|Solve the no connection puzzle]]
 
<syntaxhighlight lang="raku" perl6line>my @adjacent = [3, 0],
[2, -2], [2, 2],
[0, -3], [0, 3],
Line 1,775 ⟶ 1,776:
say "$tries tries";
}</langsyntaxhighlight>
 
{{out}}
Line 1,790 ⟶ 1,791:
 
No particular effort was made to reduce the elapsed time in solving the puzzle.
<langsyntaxhighlight lang="rexx">/*REXX program solves a Hopido puzzle, it also displays the puzzle and the solution. */
call time 'Reset' /*reset the REXX elapsed timer to zero.*/
maxR=0; maxC=0; maxX=0; minR=9e9; minC=9e9; minX=9e9; cells=0; @.=
Line 1,845 ⟶ 1,846:
say _
end /*r*/
say; return</langsyntaxhighlight>
'''output''' &nbsp; when the input is: <br>
<tt> 1 4 1 \2 3 . . . \3 2 . . . . . \4 1 . . . . . . . \5 1 . . . . . . . \6 2 . . \6 5 . . </tt>
Line 1,870 ⟶ 1,871:
=={{header|Ruby}}==
This solution uses HLPsolver from [[Solve_a_Hidato_puzzle#With_Warnsdorff | here]]
<langsyntaxhighlight lang="ruby">require 'HLPsolver'
 
ADJACENT = [[-3, 0], [0, -3], [0, 3], [3, 0], [-2, -2], [-2, 2], [2, -2], [2, 2]]
Line 1,884 ⟶ 1,885:
t0 = Time.now
HLPsolver.new(board1).solve
puts " #{Time.now - t0} sec"</langsyntaxhighlight>
Which produces:
<pre>
Line 1,908 ⟶ 1,909:
=={{header|Tcl}}==
{{works with|Tcl|8.6}}
<langsyntaxhighlight lang="tcl">package require Tcl 8.6
 
oo::class create HopidoSolver {
Line 2,003 ⟶ 2,004:
HopidoSolver create hop $puzzle
hop solve
showPuzzle [hop solution] "Output"</langsyntaxhighlight>
{{out}}
<pre>
Line 2,026 ⟶ 2,027:
{{libheader|Wren-sort}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./sort" for Sort
import "./fmt" for Fmt
 
var board = [
Line 2,124 ⟶ 2,125:
if (pos >= nRows * nCols) break
}
printResult.call()</langsyntaxhighlight>
 
{{out}}
Line 2,138 ⟶ 2,139:
=={{header|zkl}}==
This solution uses the code from [[Solve_a_Numbrix_puzzle#zkl]]
<langsyntaxhighlight lang="zkl">hi:= // 0==empty cell, X==not a cell
#<<<
" X 0 0 X 0 0 X
Line 2,158 ⟶ 2,159:
println();
puzzle.print_board();
println();</langsyntaxhighlight>
{{out}}
<pre>
9,476

edits