Solve a Numbrix puzzle: Difference between revisions
→{{header|zkl}}: marked maybe incorrect...
Thundergnat (talk | contribs) m (→{{header|Julia}}: Fix broken markup) |
(→{{header|zkl}}: marked maybe incorrect...) |
||
(7 intermediate revisions by 4 users not shown) | |||
Line 73:
=={{header|11l}}==
{{incorrect|11l|3rd solution has "00 00" in it where "02 01" shd be (as Python)}}
{{trans|Python}}
<
[Int] exists
V lastNumber = 0
Line 161 ⟶ 162:
r = solve(‘17 . . . 11 . . . 59 . 15 . . 6 . . 61 . . . 3 . . . 63 . . . . . . 66 . . . . 23 24 . 68 67 78 . 54 55’""
‘ . . . . 72 . . . . . . 35 . . . 49 . . . 29 . . 40 . . 47 . 31 . . . 39 . . . 45’, 9, 9)
show_result(r)</
{{out}}
Line 197 ⟶ 198:
=={{header|AutoHotkey}}==
<
if (R&&C) ; if neighbors (not first iteration)
{
Line 259 ⟶ 260:
}
return StrReplace(map, ">")
}</
Examples:<
Grid := [[0, 0, 0, 0, 0, 0, 0, 0, 0]
,[0, 0, 46, 45, 0, 55, 74, 0, 0]
Line 288 ⟶ 289:
return
</syntaxhighlight>
Outputs:<pre>49 50 51 52 53 54 75 76 81
48 47 46 45 44 55 74 77 80
Line 304 ⟶ 305:
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.
<
using System.Collections.Generic;
using static System.Console;
Line 445 ⟶ 446:
}
}</
{{out}}
<pre>
Line 470 ⟶ 471:
=={{header|C++}}==
<
#include <vector>
#include <sstream>
Line 626 ⟶ 627:
return system("pause");
}
</syntaxhighlight>
{{Out}}
<pre>
Line 663 ⟶ 664:
From the refactored C++ version with more precise typing. The NumbrixPuzzle struct is created at compile-time, so its asserts and exceptions can catch most malformed puzzles at compile-time.
{{trans|C++}}
<
struct {
Line 871 ⟶ 872:
writefln("One solution:\n%(%-(%2s %)\n%)\n", solution);
}
}</
{{out}}
<pre>One solution:
Line 909 ⟶ 910:
{{trans|Ruby}}
This solution uses HLPsolver from [[Solve_a_Hidato_puzzle#Elixir | here]]
<
adjacent = [{-1, 0}, {0, -1}, {0, 1}, {1, 0}]
Line 937 ⟶ 938:
0 0 0 0 0 0 0 0 0
"""
HLPsolver.solve(board2, adjacent)</
{{out}}
Line 988 ⟶ 989:
=={{header|Go}}==
{{trans|Kotlin}}
<
import (
Line 1,106 ⟶ 1,107:
}
}
}</
{{out}}
Line 1,138 ⟶ 1,139:
This is a Unicon-specific solution, based on the Unicon Hidato problem solver:
<
record Pos(r,c)
Line 1,227 ⟶ 1,228:
QMouse(puzzle, visit(loc.r+1,loc.c), self, val) # South
QMouse(puzzle, visit(loc.r, loc.c-1), self, val) # West
end</
{{Out}}Sample runs:
Line 1,286 ⟶ 1,287:
=={{header|Java}}==
{{works with|Java|8}}
<
public class Numbrix {
Line 1,376 ⟶ 1,377:
}
}
}</
<pre>49 50 51 52 53 54 75 76 81
Line 1,390 ⟶ 1,391:
=={{header|Julia}}==
See the Hidato module [[Solve_a_Hidato_puzzle#Julia | here]].
<
const numbrixmoves = [[-1, 0], [0, -1], [0, 1], [1, 0]]
Line 1,403 ⟶ 1,404:
hidatosolve(board, maxmoves, numbrixmoves, fixed, starts[1][1], starts[1][2], 1)
printboard(board)
</
0 0 0 0 0 0 0 0 0
0 0 46 45 0 55 74 0 0
Line 1,447 ⟶ 1,448:
Uses the Hidato puzzle solver module, which has its source code listed [[Solve_a_Hidato_puzzle#Julia | here]] in the Hadato task.
<
const numbrix1 = """
Line 1,482 ⟶ 1,483:
hidatosolve(board, maxmoves, numbrixmoves, fixed, starts[1][1], starts[1][2], 1)
printboard(board)
</syntaxhighlight>
{{output}}
<pre>
Line 1,528 ⟶ 1,529:
=={{header|Kotlin}}==
{{trans|Java}}
<
val example1 = listOf(
Line 1,617 ⟶ 1,618:
if (solve(startRow, startCol, 1, 0)) printResult(n + 1)
}
}</
{{out}}
Line 1,647 ⟶ 1,648:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
NakedN, HiddenN, ChainSearch, HidatoSolve, Cornering, ValidPuzzle, \
GapSearch, ReachDelete, GrowNeighbours]
Line 2,012 ⟶ 2,013:
indices = Flatten[Position[cells, #] & /@ clues[[All, 1]]];
candidates[[indices]] = clues[[All, 2]];
out = HidatoSolve[cells, candidates];</
{{out}}
Outputs a graphical representation of the two numbrix puzzles and their solutions.
Line 2,019 ⟶ 2,020:
{{trans|Go}}
With many changes, for instance using a “Numbrix” object as context, adding a procedure to create this object, etc.
<
const Moves = [(1, 0), (0, 1), (-1, 0), (0, -1)]
Line 2,112 ⟶ 2,113:
numbrix.print()
else:
echo "No solution."</
{{out}}
Line 2,141 ⟶ 2,142:
=={{header|Perl}}==
Tested on perl v5.26.1
<
use strict;
Line 2,180 ⟶ 2,181:
($_ = $in) =~ s/00(?=.{$gap}$have)/$want/s and solve( $want, $_ ); # U
}
}</
{{out|case=Example 1}}
Line 2,207 ⟶ 2,208:
=={{header|Phix}}==
<!--(phixonline)-->
<syntaxhighlight lang="phix">
with javascript_semantics
include sets.e
sequence board, placed, px, py
integer w, h, limit, missing
bool solved
function get_moves(integer n)
sequence res = {}
integer x = px[n], y = py[n]
if x>1 and board[y,x-1]=0 then res &= {{x-1,y}} end if
if x<w and board[y,x+1]=0 then res &= {{x+1,y}} end if
if y>1 and board[y-1,x]=0 then res &= {{x,y-1}} end if
if y<h and board[y+1,x]=0 then res &= {{x,y+1}} end if
return res
end function
procedure solve()
if missing=0 then
else
-- scan for next to place, which will be the lowest
-- of those with either n+1 or n-1 already placed,
-- checking that all
integer place
sequence
for n=limit to 1
if not
if n<limit and placed[n+1] then
place = n
plus1 = true
moves = get_moves(n+1)
if length(moves)=0 then
return -- fail/backtrack
end if
end if
if n>1 and placed[n-1] then
place = n
if plus1 then
moves = intersection(moves,get_moves(n-1))
else
moves = get_moves(n-1)
end if
if length(moves)=0 then
return -- fail/backtrack
end if
end if
end if
end for
for m in moves do
integer {x,y} = m
px[place] = x
py[place] = y
board[y,x] = place
placed[place] = true
solve()
if solved then return end if
placed[place] = false
board[y,x] = 0
end for
missing += 1
end if
end procedure
procedure Numbrix(string s)
atom t0 = time()
board =
for i,line
end for
w = length(board[1])
h = length(board)
limit = w*h
placed = repeat(false,limit)
px = repeat(0,limit)
for y=1 to h do
integer byx = board[y][x]
if byx then
placed[byx] = true
px[byx] = x
py[byx] = y
missing += 1
end if
end for
end for
if not solved then
puts(1,"No solutions\n\n")
else
string fmt = sprintf(" %%%dd",nchars)
printf(1,"solution found in %s:\n\n",elapsed(time()-t0))
board = apply(true,join_by,{board,1,w,{""},{""},{fmt}})
printf(1,"%s\n\n",{join(board,"\n")})
end if
end procedure
constant
. . . . . . . . .
. . 46 45 . 55 74 . .
Line 2,295 ⟶ 2,323:
. 18 . . 11 . . 64 .
. . 24 21 . 1 2 . .
. . . . . . . . .""","""
. . . . . . . . .
. 11 12 15 18 21 62 61 .
Line 2,307 ⟶ 2,332:
. 38 . . . . . 72 .
. 43 44 47 48 51 76 77 .
. . . . . . . . .""","""
17 . . . 11 . . . 59
. 15 . . 6 . . 61 .
. . 3 . . . 63 . .
. . . . 66 . . . .
23 24 . 68 67 78 . 54 55
. . . . 72 . . . .
. . 35 . . . 49 . .
. 29 . . 40 . . 47 .
31 . . . 39 . . . 45""","""
109 0 0 0 0 0 0 0 0 0 0 0 0 0 43
0 0 0 0 0 0 0 65 0 0 0 0 0 0 0
0 0 101 100 0 92 0 76 0 68 0 48 3 0 0
0 0 102 97 0 0 80 0 74 0 0 49 6 0 0
0 0 0 0 0 0 79 0 73 0 0 0 0 0 0
0 0 116 0 0 0 0 0 0 0 0 0 10 0 0
0 0 0 118 217 0 0 0 0 0 55 52 0 0 0
0 121 120 0 0 0 0 213 0 0 0 0 12 35 0
0 0 0 166 167 0 0 0 0 0 205 204 0 0 0
0 0 162 0 0 0 0 0 0 0 0 0 14 0 0
0 0 0 0 0 0 173 0 177 0 0 0 0 0 0
0 0 156 153 0 0 150 0 178 0 0 201 16 0 0
0 0 155 154 0 144 0 180 0 188 0 200 17 0 0
0 0 0 0 0 0 0 183 0 0 0 0 0 0 0
135 0 0 0 0 0 0 0 0 0 0 0 0 0 21"""}
papply(boards,Numbrix)
</syntaxhighlight>
{{out}}
<pre>
. . . . . . . . .
. . 46 45 . 55 74 . .
. 38 . . 43 . . 78 .
. 35 . . . . . 71 .
. . 33 . . . 59 . .
. 17 . . . . . 67 .
. 18 . . 11 . . 64 .
. . 24 21 . 1 2 . .
. . . . . . . . .
solution found in 0.1s:
49 50 51 52 53 54 75 76 81
48 47 46 45 44 55 74 77 80
Line 2,320 ⟶ 2,382:
28 25 24 21 10 1 2 3 4
27 26 23 22 9 8 7 6 5
. . . . . . . . .
. 11 12 15 18 21 62 61 .
. 6 . . . . . 60 .
. 33 . . . . . 57 .
. 32 . . . . . 56 .
. 37 . 1 . . . 73 .
. 38 . . . . . 72 .
. 43 44 47 48 51 76 77 .
. . . . . . . . .
solution found in 0.0s:
9 10 13 14 19 20 63 64 65
8 11 12 15 18 21 62 61 66
Line 2,330 ⟶ 2,404:
40 43 44 47 48 51 76 77 78
41 42 45 46 49 50 81 80 79
17 . . . 11 . . . 59
. 15 . . 6 . . 61 .
. . 3 . . . 63 . .
. . . . 66 . . . .
23 24 . 68 67 78 . 54 55
. . . . 72 . . . .
. . 35 . . . 49 . .
. 29 . . 40 . . 47 .
31 . . . 39 . . . 45
solution found in 0.0s:
17 16 13 12 11 10 9 60 59
18 15 14 5 6 7 8 61 58
19 20 3 4 65 64 63 62 57
22 21 2 1 66 79 80 81 56
23 24 69 68 67 78 77 54 55
26 25 70 71 72 75 76 53 52
27 28 35 36 73 74 49 50 51
30 29 34 37 40 41 48 47 46
31 32 33 38 39 42 43 44 45
109 0 0 0 0 0 0 0 0 0 0 0 0 0 43
0 0 0 0 0 0 0 65 0 0 0 0 0 0 0
0 0 101 100 0 92 0 76 0 68 0 48 3 0 0
0 0 102 97 0 0 80 0 74 0 0 49 6 0 0
0 0 0 0 0 0 79 0 73 0 0 0 0 0 0
0 0 116 0 0 0 0 0 0 0 0 0 10 0 0
0 0 0 118 217 0 0 0 0 0 55 52 0 0 0
0 121 120 0 0 0 0 213 0 0 0 0 12 35 0
0 0 0 166 167 0 0 0 0 0 205 204 0 0 0
0 0 162 0 0 0 0 0 0 0 0 0 14 0 0
0 0 0 0 0 0 173 0 177 0 0 0 0 0 0
0 0 156 153 0 0 150 0 178 0 0 201 16 0 0
0 0 155 154 0 144 0 180 0 188 0 200 17 0 0
0 0 0 0 0 0 0 183 0 0 0 0 0 0 0
135 0 0 0 0 0 0 0 0 0 0 0 0 0 21
solution found in 0.5s:
109 108 87 86 85 84 83 64 63 62 61 46 45 44 43
110 107 88 89 90 91 82 65 66 67 60 47 2 1 42
111 106 101 100 99 92 81 76 75 68 59 48 3 4 41
112 105 102 97 98 93 80 77 74 69 58 49 6 5 40
113 104 103 96 95 94 79 78 73 70 57 50 7 8 39
114 115 116 225 224 223 222 221 72 71 56 51 10 9 38
123 122 117 118 217 218 219 220 209 208 55 52 11 36 37
124 121 120 119 216 215 214 213 210 207 54 53 12 35 34
125 164 165 166 167 168 169 212 211 206 205 204 13 32 33
126 163 162 161 160 171 170 175 176 191 192 203 14 31 30
127 128 157 158 159 172 173 174 177 190 193 202 15 28 29
130 129 156 153 152 151 150 179 178 189 194 201 16 27 26
131 132 155 154 143 144 149 180 181 188 195 200 17 24 25
134 133 138 139 142 145 148 183 182 187 196 199 18 23 22
135 136 137 140 141 146 147 184 185 186 197 198 19 20 21
</pre>
=={{header|Picat}}==
<syntaxhighlight lang="picat">
import sat, util.
main([File]) =>
Lines = read_file_lines(File),
Dim = Lines.len(),
Board = new_array(Dim, Dim),
Max = Dim*Dim,
Board :: 1..Max,
Bvars = Board.vars(),
all_different(Bvars),
foreach ( R in 1..Dim )
Line = Lines[R].split(),
if( Line.len() != Dim ) then
printf("Line %d too short or too long, failing\n", R),
abort
end,
foreach ( C in 1..Dim ) % empty cell: _ or 0
if ( Line[C] != ['_'] ) then % data as 49 _ _ 32 _ _...
Num = Line[C].to_int(),
if ( Num != 0 ) then % data as 0 11 12 15 18...
Board[R,C] #= Num
end
end
end
end,
% each cell but that with value 1 must be +1 larger then one of its neighbours
% some numbrix puzzles do not have min and/or max values,
% but this method works for all cases
foreach ( R in 1..Dim, C in 1..Dim )
Nei = [(R1,C1) : (R1, C1) in [(R-1,C), (R,C+1), (R+1,C), (R,C-1)],
between(1, Dim, R1), between(1, Dim, C1)],
Consnei = [ Board[R,C] #= Board[R1,C1] + 1 : (R1,C1) in Nei ],
Board[R,C] #!= 1 #=> sum(Consnei) #= 1
end,
time2(solve(Bvars)),
printboard(Board).
printboard(A) =>
N = A.len,
nl,
foreach ( I in 1..N )
foreach ( J in 1..A[I].len )
if ( A[I,J] == 0 ) then
printf(" ")
else
printf("%4w", A[I,J])
end
end,
nl
end.
</syntaxhighlight>
{{out}}
<pre>
Solution 1:
49 50 51 52 53 54 75 76 81
48 47 46 45 44 55 74 77 80
37 38 39 40 43 56 73 78 79
36 35 34 41 42 57 72 71 70
31 32 33 14 13 58 59 68 69
30 17 16 15 12 61 60 67 66
29 18 19 20 11 62 63 64 65
28 25 24 21 10 1 2 3 4
27 26 23 22 9 8 7 6 5
Solution 2:
9 10 13 14 19 20 63 64 65
8 11 12 15 18 21 62 61 66
7 6 5 16 17 22 59 60 67
34 33 4 3 24 23 58 57 68
35 32 31 2 25 54 55 56 69
36 37 30 1 26 53 74 73 70
39 38 29 28 27 52 75 72 71
40 43 44 47 48 51 76 77 78
41 42 45 46 49 50 81 80 79
Problem, no starting (1) nor end (225) points (2.344 seconds):
109 0 0 0 0 0 0 0 0 0 0 0 0 0 43
0 0 0 0 0 0 0 65 0 0 0 0 0 0 0
0 0 101 100 0 92 0 76 0 68 0 48 3 0 0
0 0 102 97 0 0 80 0 74 0 0 49 6 0 0
0 0 0 0 0 0 79 0 73 0 0 0 0 0 0
0 0 116 0 0 0 0 0 0 0 0 0 10 0 0
0 0 0 118 217 0 0 0 0 0 55 52 0 0 0
0 121 120 0 0 0 0 213 0 0 0 0 12 35 0
0 0 0 166 167 0 0 0 0 0 205 204 0 0 0
0 0 162 0 0 0 0 0 0 0 0 0 14 0 0
0 0 0 0 0 0 173 0 177 0 0 0 0 0 0
0 0 156 153 0 0 150 0 178 0 0 201 16 0 0
0 0 155 154 0 144 0 180 0 188 0 200 17 0 0
0 0 0 0 0 0 0 183 0 0 0 0 0 0 0
135 0 0 0 0 0 0 0 0 0 0 0 0 0 21
Solution:
109 108 87 86 85 84 83 64 63 62 61 46 45 44 43
110 107 88 89 90 91 82 65 66 67 60 47 2 1 42
111 106 101 100 99 92 81 76 75 68 59 48 3 4 41
112 105 102 97 98 93 80 77 74 69 58 49 6 5 40
113 104 103 96 95 94 79 78 73 70 57 50 7 8 39
114 115 116 225 224 223 222 221 72 71 56 51 10 9 38
123 122 117 118 217 218 219 220 209 208 55 52 11 36 37
124 121 120 119 216 215 214 213 210 207 54 53 12 35 34
125 164 165 166 167 168 169 212 211 206 205 204 13 32 33
126 163 162 161 160 171 170 175 176 191 192 203 14 31 30
127 128 157 158 159 172 173 174 177 190 193 202 15 28 29
130 129 156 153 152 151 150 179 178 189 194 201 16 27 26
131 132 155 154 143 144 149 180 181 188 195 200 17 24 25
134 133 138 139 142 145 148 183 182 187 196 199 18 23 22
135 136 137 140 141 146 147 184 185 186 197 198 19 20 21
</pre>
=={{header|Prolog}}==
<
* Solver
*/
Line 2,440 ⟶ 2,685:
_, 29, _, _, 40, _, _, 47, _,
31, _, _, _, 39, _, _, _, 45
]).</
{{out}}
<pre>
Line 2,483 ⟶ 2,728:
=={{header|Python}}==
{{incorrect|Python|3rd solution has "00 00" in it where "02 01" shd be}}
<syntaxhighlight lang="python">
from sys import stdout
neighbours = [[-1, 0], [0, -1], [1, 0], [0, 1]]
Line 2,582 ⟶ 2,828:
" . . . . 72 . . . . . . 35 . . . 49 . . . 29 . . 40 . . 47 . 31 . . . 39 . . . 45", 9, 9)
show_result(r)
</
49 50 51 52 53 54 75 76 81
48 47 46 45 44 55 74 77 80
Line 2,623 ⟶ 2,869:
<code>hidato-family-solver.rkt</code>
<
;;; Used in my solutions of:
;;; "Solve a Hidato Puzzle"
Line 2,717 ⟶ 2,963:
[(solution-starting-at 0) => values]))
(and sltn (hash->puzzle sltn)))</
<
(require "hidato-family-solver.rkt")
Line 2,753 ⟶ 2,999:
#(0 38 0 0 0 0 0 72 0)
#(0 43 44 47 48 51 76 77 0)
#(0 0 0 0 0 0 0 0 0)))))</
{{out}}
Line 2,787 ⟶ 3,033:
* [[Solve the no connection puzzle#Raku|Solve the no connection puzzle]]
<syntaxhighlight lang="raku"
[ 0, -1], [ 0, 1],
[ 1, 0];
Line 2,903 ⟶ 3,149:
say "$tries tries";
}
</syntaxhighlight>
{{out}}
Line 2,937 ⟶ 3,183:
''Hidato'' and ''Numbrix'' are registered trademarks.
<
maxR= 0; maxC= 0; maxX= 0; /*define maxR, maxC, and maxX. */
minR= 9e9; minC= 9e9; minX= 9e9; /* " minR, minC, " minX. */
Line 2,996 ⟶ 3,242:
say _
end /*r*/
say; return</
{{out|output|text= when using the input of:}} <br>
<tt> 1 1 . . . . . . . . ./2 1 . . 24 21 . 1 2 . ./3 1 . 18 . . 11 . . 64 ./4 1 . 17 . . . . . 67 ./5 1 . . 33 . . . 59 . ./6 1 . 35 . . . . . 71 ./7 1 . 38 . . 43 . . 78 ./8 1 . . 46 45 . 55 74 . ./9 1 . . . . . . . . . </tt>
Line 3,051 ⟶ 3,297:
=={{header|Ruby}}==
This solution uses HLPsolver from [[Solve_a_Hidato_puzzle#With_Warnsdorff | here]]
<
ADJACENT = [[-1, 0], [0, -1], [0, 1], [1, 0]]
Line 3,079 ⟶ 3,325:
0 0 0 0 0 0 0 0 0
EOS
HLPsolver.new(board2).solve</
Which produces:
<pre>
Line 3,129 ⟶ 3,375:
=={{header|SystemVerilog}}==
<
//////////////////////////////////////////////////////////////////////////////
Line 3,255 ⟶ 3,501:
end
endprogram
</syntaxhighlight>
Running the above program in ncverilog
Line 3,285 ⟶ 3,531:
Following loosely the structure of [[Solve_a_Hidato_puzzle#Tcl]].
<
# Example:
# % eachpair {a b} {1 2 3} {puts $a $b}
Line 3,481 ⟶ 3,727:
puts "\n== No Solution for Puzzle $i =="
}
}</
{{Out}}
Line 3,533 ⟶ 3,779:
{{libheader|Wren-sort}}
{{libheader|Wren-fmt}}
<
import "./fmt" for Fmt
var example1 = [
Line 3,621 ⟶ 3,867:
if (solve.call(startRow, startCol, 1, 0)) printResult.call(n + 1)
n = n + 1
}</
{{out}}
Line 3,651 ⟶ 3,897:
=={{header|zkl}}==
{{incorrect|zkl|[Maybe], see python issue, which is not evident in the output here...}}
{{trans|Python}}
This code solves Hidato, Hopido and Numbrix puzzles.
<
class Puzzle{ // hold info concerning this puzzle
var board, nrows,ncols, cells,
Line 3,710 ⟶ 3,957:
False
}
} // Puzzle</
<
#<<<
"0 0 0 0 0 0 0 0 0
Line 3,747 ⟶ 3,994:
puzzle.print_board();
println();
}</
{{out}}
<pre>
|