Nonogram solver: Difference between revisions

m
Line 1,489:
end
 
ycols2xrows(ycols) = [[ycols[i][j] for i in eachindex(ycols)] for j in eachindex(ycols[1])]
function ycols2xrows(ycols)
xrows = Vector{Vector{Int}}()
for j in eachindex(ycols[1])
row = Vector{Int}()
for i in eachindex(ycols)
push!(row, ycols[i][j])
end
push!(xrows, row)
end
xrows
end
 
function hintsfromcol(rowvec, col, nrows)
Line 1,548 ⟶ 1,538:
ensurevecvec(arr::Vector{Vector{Int}}) = arr
ensurevecvec(arr::Vector{Int}) = [arr]
function excludethemexcl!(choices, otherchoices)
 
function excludethem!(choices, otherchoices)
for i in 1:length(choices)
if length(choices[i]) > 0
Line 1,559 ⟶ 1,548:
end
end
xclude!(x, y) = (excl!(x, y); x = map(ensurevecvec, x); y = map(ensurevecvec, y); (x, y))
 
xlen, ylen = sum(map(length, xchoices)), sum(map(length, ychoices))
while true
excludethemychoices, xchoices = xclude!(ychoices, xchoices)
xchoices = map(i -> ensurevecvec(i), xchoices)
ychoices = map(i -> ensurevecvec(i), ychoices)
if any(isempty, xchoices)
return
end
excludethemxchoices, ychoices = xclude!(xchoices, ychoices)
xchoices = map(i -> ensurevecvec(i), xchoices)
ychoices = map(i -> ensurevecvec(i), ychoices)
if any(isempty, ychoices)
return
Line 1,636 ⟶ 1,621:
xh, yh = map(x -> txtline2ints(strip(x)), split(xyhints, "\n"))
nonogram = NonogramPuzzle(xh, yh)
println("\nPuzzle $(pcount += 1):")
println("\nPuzzle $pcount:")
trygrids(nonogram)
foreach(x -> println(solutiontxt(x), "\n"), nonogram.solutions)
println()
end
end
4,102

edits