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>function hidatoconfigure(str)
<lang julia>module Hidato

lines = map(x -> x * " ", split(str, "\n"))
export hidatosolve, printboard, hidatoconfigure
nrows, ncols = length(lines), length(lines[1])

board = fill(-1, (nrows, div(ncols, 3)))
function hidatoconfigure(str)
lines = split(str, "\n")
nrows, ncols = length(lines), length(split(strip(lines[1]), r"\s+"))
board = fill(-1, (nrows, ncols))
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)
if (ch = lines[i][3 * (j-1) + 2]) == '_'
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
elseif ch == '.'
blanks += 1
elseif c == '.'
continue
continue
else # numeral, get 2 digits
else # numeral, get 2 digits
board[i, j] = parse(Int, lines[i][3*(j-1)+1:3*(j-1)+2])
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
startpos = [i, j]
push!(starts, CartesianIndex(i, j))
end
end
end
end
end
end
board, sort!(presets), startpos
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 > fixed[end]
if sought > maxmove
return true
return true
elseif (board[row, col] != 0 && board[row, col] != sought) || (board[row, col] == 0 && fixed[next] == 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
maxrow, maxcol = size(board)
for move in movematrix
for i in max(row - 1, 1):min(row + 1, maxrow), j in max(col -1, 1):min(col + 1, maxcol)
i, j = row + move[1], col + move[2]
if !(i == row && j == col) && hidatosolve(board, fixed, i, j, sought + 1, next)
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 => " ", 0 => "__ ", -2 => "\n")
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, fixed, start = hidatoconfigure(hidat)

board, empties, fixed, starts = hidatoconfigure(hidat)
printboard(board)
printboard(board)
hidatosolve(board, fixed, start[1], start[2], 1, 1)
hidatosolve(board, kingmoves, fixed, starts[1][1], starts[1][2], 1, 1, fixed[end])
printboard(board)
printboard(board)
</lang>{{output}}<pre>
</lang>{{output}}<pre>