Solve a Hidato puzzle: Difference between revisions

m
Line 1,908:
{{trans|Python}}
<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"))
lengthsnrows, ncols = map(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)))
presets = Vector{Int}()
startpos = [0, 0]
for i in 1:nrows, j in 1:div(ncols, 3)
if (ch = lines[i][3 * (j-1) + 2]) == '_'
if ch == '_'
board[i, j] = 0
elseif ch == '.'
Line 1,941 ⟶ 1,932:
if sought > fixed[end]
return true
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
end
Line 1,953 ⟶ 1,943:
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)
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
end
Line 1,965 ⟶ 1,953:
function printboard(board)
d = Dict(-1 => " ", 0 => "__ ", -2 => "\n")
bmaxmap(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]))'], ""))
end
4,102

edits