Solve a Hidato puzzle: Difference between revisions

Line 1,913:
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}()
starts = Vector{CartesianIndex{2}}()
blanksmaxmoves = 0
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
blanksmaxmoves += 1
elseif c == '.'
continue
Line 1,931:
push!(starts, CartesianIndex(i, j))
end
maxmoves += 1
end
end
board, blanksmaxmoves, sort!(presets), length(starts) == 1 ? starts : findall(x -> x == 0, board)
end
 
function hidatosolve(board, maxmoves, movematrix, fixed, row, col, sought, next, maxmove)
if sought > maxmovemaxmoves
return true
elseif (0 != board[row, col] != sought) || (board[row, col] == 0 && sought in fixed)
Line 1,951 ⟶ 1,952:
i, j = row + move[1], col + move[2]
if (0 < i <= size(board)[1]) && (0 < j <= size(board)[2]) &&
hidatosolve(board, maxmoves, movematrix, fixed, i, j, sought + 1, next, maxmove)
return true
end
Line 1,965 ⟶ 1,966:
end
 
end # modulemodules
</lang><lang julia>using .Hidato
 
using .Hidato
 
hidat = """
Line 1,981:
const kingmoves = [[-1, -1], [-1, 0], [-1, 1], [0, -1], [0, 1], [1, -1], [1, 0], [1, 1]]
 
board, emptiesmaxmoves, fixed, starts = hidatoconfigure(hidat)
printboard(board)
hidatosolve(board, maxmoves, kingmoves, fixed, starts[1][1], starts[1][2], 1, 1, fixed[end])
printboard(board)
</lang>{{output}}<pre>
4,102

edits