Solve a Hidato puzzle: Difference between revisions
Content added Content deleted
(Add Rust implementation) |
Alextretyak (talk | contribs) (Added 11l) |
||
Line 31: | Line 31: | ||
* [[Solve the no connection puzzle]]; |
* [[Solve the no connection puzzle]]; |
||
<br><br> |
<br><br> |
||
=={{header|11l}}== |
|||
{{trans|Python}} |
|||
<lang 11l>[[Int]] board |
|||
[Int] given |
|||
V start = (-1, -1) |
|||
F setup(s) |
|||
V lines = s.split("\n") |
|||
V ncols = lines[0].split(‘ ’, group_delimiters' 1B).len |
|||
V nrows = lines.len |
|||
:board = (0 .< nrows + 2).map(_ -> [-1] * (@ncols + 2)) |
|||
L(row) lines |
|||
V r = L.index |
|||
L(cell) row.split(‘ ’, group_delimiters' 1B) |
|||
V c = L.index |
|||
I cell == ‘__’ |
|||
:board[r + 1][c + 1] = 0 |
|||
L.continue |
|||
E I cell == ‘.’ |
|||
L.continue |
|||
E |
|||
V val = Int(cell) |
|||
:board[r + 1][c + 1] = val |
|||
:given.append(val) |
|||
I val == 1 |
|||
:start = (r + 1, c + 1) |
|||
:given.sort() |
|||
F solve(r, c, n, =next = 0) |
|||
I n > :given.last |
|||
R 1B |
|||
I :board[r][c] & :board[r][c] != n |
|||
R 0B |
|||
I :board[r][c] == 0 & :given[next] == n |
|||
R 0B |
|||
V back = 0 |
|||
I :board[r][c] == n |
|||
next++ |
|||
back = n |
|||
:board[r][c] = n |
|||
L(i) -1 .< 2 |
|||
L(j) -1 .< 2 |
|||
I solve(r + i, c + j, n + 1, next) |
|||
R 1B |
|||
:board[r][c] = back |
|||
R 0B |
|||
F print_board() |
|||
V d = [-1 = ‘ ’, 0 = ‘__’] |
|||
V bmax = max(:board.map(r -> max(r))) |
|||
V lbmax = String(bmax).len + 1 |
|||
L(r) :board[1 .< (len)-1] |
|||
print(r[1 .< (len)-1].map(c -> @d.get(c, String(c)).rjust(@lbmax)).join(‘’)) |
|||
V hi = |
|||
‘__ 33 35 __ __ . . . |
|||
__ __ 24 22 __ . . . |
|||
__ __ __ 21 __ __ . . |
|||
__ 26 __ 13 40 11 . . |
|||
27 __ __ __ 9 __ 1 . |
|||
. . __ __ 18 __ __ . |
|||
. . . . __ 7 __ __ |
|||
. . . . . . 5 __’ |
|||
setup(hi) |
|||
print_board() |
|||
solve(start[0], start[1], 1) |
|||
print() |
|||
print_board()</lang> |
|||
{{out}} |
|||
<pre> |
|||
__ 33 35 __ __ |
|||
__ __ 24 22 __ |
|||
__ __ __ 21 __ __ |
|||
__ 26 __ 13 40 11 |
|||
27 __ __ __ 9 __ 1 |
|||
__ __ 18 __ __ |
|||
__ 7 __ __ |
|||
5 __ |
|||
32 33 35 36 37 |
|||
31 34 24 22 38 |
|||
30 25 23 21 12 39 |
|||
29 26 20 13 40 11 |
|||
27 28 14 19 9 10 1 |
|||
15 16 18 8 2 |
|||
17 7 6 3 |
|||
5 4 |
|||
</pre> |
|||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |