Solve a Hidato puzzle: Difference between revisions

Content added Content deleted
Line 1,908: Line 1,908:
{{trans|Python}}
{{trans|Python}}
<lang julia>function hidatoconfigure(str)
<lang julia>function hidatoconfigure(str)
if match(r"[^\s\.\_\d]", str) != nothing
throw("Bad Hidato: ($str) Characters must be whitespace, _, ., or digit")
end
lines = map(x -> x * " ", split(str, "\n"))
lines = map(x -> x * " ", split(str, "\n"))
lengths = map(length, lines)
nrows, ncols = length(lines), length(lines[1])
nrows, ncols = length(lines), lengths[1]
if ncols % 3 != 0 || maximum(map(length, lines)) != minimum(map(length, lines))
throw("Bad Hidato: ($str) Nonrectangular or line length not multiple of 3")
end
board = fill(-1, (nrows, div(ncols, 3)))
board = fill(-1, (nrows, div(ncols, 3)))
presets = Vector{Int}()
presets = Vector{Int}()
startpos = [0, 0]
startpos= [0, 0]
for i in 1:nrows, j in 1:div(ncols, 3)
for i in 1:nrows, j in 1:div(ncols, 3)
ch = lines[i][3 * (j-1) + 2]
if (ch = lines[i][3 * (j-1) + 2]) == '_'
if ch == '_'
board[i, j] = 0
board[i, j] = 0
elseif ch == '.'
elseif ch == '.'
Line 1,941: Line 1,932:
if sought > fixed[end]
if sought > fixed[end]
return true
return true
elseif (board[row, col] != 0 && board[row, col] != sought) ||
elseif (board[row, col] != 0 && board[row, col] != sought) || (board[row, col] == 0 && fixed[next] == sought)
(board[row, col] == 0 && fixed[next] == sought)
return false
return false
end
end
Line 1,953: Line 1,943:
maxrow, maxcol = size(board)
maxrow, maxcol = size(board)
for i in max(row - 1, 1):min(row + 1, maxrow), j in max(col -1, 1):min(col + 1, maxcol)
for i in max(row - 1, 1):min(row + 1, maxrow), j in max(col -1, 1):min(col + 1, maxcol)
if i == row && j == col
if !(i == row && j == col) && hidatosolve(board, fixed, i, j, sought +1, next)
continue
elseif hidatosolve(board, fixed, i, j, sought +1, next)
return true
return true
end
end
Line 1,965: Line 1,953:
function printboard(board)
function printboard(board)
d = Dict(-1 => " ", 0 => "__ ", -2 => "\n")
d = Dict(-1 => " ", 0 => "__ ", -2 => "\n")
bmax = maximum(board)
map(x -> d[x] = rpad(lpad(string(x), 2), 3), 1:maximum(board))
map(x -> d[x] = rpad(lpad(string(x), 2), 3), 1:bmax)
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