Solve a Numbrix puzzle: Difference between revisions

m (→‎{{header|Wren}}: Minor tidy)
Line 2,334:
41 42 45 46 49 50 81 80 79
solution found in 334 tries (0.00s)
</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>
 
1

edit