Solve a Hidato puzzle: Difference between revisions
Content added Content deleted
Line 1,906: | Line 1,906: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
This solution utilizes a module which is loosely based on the Python version, and is also used for the Hopido task. |
|||
{{trans|Python}} |
|||
<lang julia> |
<lang julia>module Hidato |
||
⚫ | |||
export hidatosolve, printboard, hidatoconfigure |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
presets = Vector{Int}() |
presets = Vector{Int}() |
||
starts = Vector{CartesianIndex{2}}() |
|||
startpos= [0, 0] |
|||
blanks = 0 |
|||
for i in 1:nrows, j in 1:div(ncols, 3) |
|||
for (i, line) in enumerate(lines), (j, s) in enumerate(split(strip(line), r"\s+")) |
|||
c = s[1] |
|||
if c == '_' || (c == '0' && length(s) == 1) |
|||
board[i, j] = 0 |
board[i, j] = 0 |
||
blanks += 1 |
|||
elseif c == '.' |
|||
continue |
continue |
||
else # numeral, get 2 digits |
else # numeral, get 2 digits |
||
board[i, j] = parse(Int, |
board[i, j] = parse(Int, s) |
||
push!(presets, board[i, j]) |
push!(presets, board[i, j]) |
||
if board[i, j] == 1 |
if board[i, j] == 1 |
||
push!(starts, CartesianIndex(i, j)) |
|||
end |
end |
||
end |
end |
||
end |
end |
||
board, sort!(presets), |
board, blanks, sort!(presets), length(starts) == 1 ? starts : findall(x -> x == 0, board) |
||
end |
end |
||
function hidatosolve(board, fixed, row, col, sought, next) |
function hidatosolve(board, movematrix, fixed, row, col, sought, next, maxmove) |
||
if sought > |
if sought > maxmove |
||
return true |
return true |
||
elseif (board[row, col] != 0 && board[row, col] != sought) || |
elseif (board[row, col] != 0 && board[row, col] != sought) || |
||
(length(fixed) > 1 && board[row, col] == 0 && fixed[next] == sought) |
|||
return false |
return false |
||
end |
end |
||
Line 1,941: | Line 1,949: | ||
end |
end |
||
board[row, col] = sought # try board with this cell set to next number |
board[row, col] = sought # try board with this cell set to next number |
||
for move in movematrix |
|||
i, j = row + move[1], col + move[2] |
|||
if |
if (0 < i <= size(board)[1]) && (0 < j <= size(board)[2]) && |
||
hidatosolve(board, movematrix, fixed, i, j, sought + 1, next, maxmove) |
|||
return true |
return true |
||
end |
end |
||
Line 1,951: | Line 1,960: | ||
end |
end |
||
function printboard(board) |
function printboard(board, emptysquare= "__ ", blocked = " ") |
||
d = Dict(-1 => |
d = Dict(-1 => blocked, 0 => emptysquare, -2 => "\n") |
||
map(x -> d[x] = rpad(lpad(string(x), 2), 3), 1:maximum(board)) |
map(x -> d[x] = rpad(lpad(string(x), 2), 3), 1:maximum(board)) |
||
println(join([d[i] for i in hcat(board, fill(-2, size(board)[1]))'], "")) |
println(join([d[i] for i in hcat(board, fill(-2, size(board)[1]))'], "")) |
||
end |
end |
||
end # module |
|||
using .Hidato |
|||
hidat = """ |
hidat = """ |
||
Line 1,967: | Line 1,980: | ||
. . . . . . 5 __""" |
. . . . . . 5 __""" |
||
const kingmoves = [[-1, -1], [-1, 0], [-1, 1], [0, -1], [0, 1], [1, -1], [1, 0], [1, 1]] |
|||
⚫ | |||
board, empties, fixed, starts = hidatoconfigure(hidat) |
|||
printboard(board) |
printboard(board) |
||
hidatosolve(board, fixed, |
hidatosolve(board, kingmoves, fixed, starts[1][1], starts[1][2], 1, 1, fixed[end]) |
||
printboard(board) |
printboard(board) |
||
</lang>{{output}}<pre> |
</lang>{{output}}<pre> |