Solve a Hidato puzzle: Difference between revisions
→{{header|Julia}}
Line 1,906:
=={{header|Julia}}==
This solution utilizes a module which is loosely based on the Python version, and is also used for the Hopido task.
<lang julia>
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)))▼
▲ nrows, ncols = length(lines), length(split(strip(lines[1]), r"\s+"))
presets = Vector{Int}()
starts = Vector{CartesianIndex{2}}()
blanks = 0
for (i, line) in
c = s[1]
if c == '_' || (c == '0' && length(s) == 1)
board[i, j] = 0
elseif c == '.'
continue
else # numeral, get 2 digits
board[i, j] = parse(Int,
push!(presets, board[i, j])
if board[i, j] == 1
end
end
end
board, blanks, sort!(presets),
end
function hidatosolve(board, movematrix, fixed, row, col, sought, next, maxmove)
if sought >
return true
elseif (board[row, col] != 0 && board[row, col] != sought) ||
(length(fixed) > 1 && board[row, col] == 0 && fixed[next] == sought)
return false
end
Line 1,941 ⟶ 1,949:
end
board[row, col] = sought # try board with this cell set to next number
if
hidatosolve(board, movematrix, fixed, i, j, sought + 1, next, maxmove)
return true
end
Line 1,951 ⟶ 1,960:
end
function printboard(board, emptysquare= "__ ", blocked = " ")
d = Dict(-1 =>
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]))'], ""))
end
end # module
using .Hidato
hidat = """
Line 1,967 ⟶ 1,980:
. . . . . . 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)
hidatosolve(board, kingmoves, fixed,
printboard(board)
</lang>{{output}}<pre>
|