Word search: Difference between revisions

(Added Go)
Line 1,291:
liz (9,8)(7,8) dam (2,0)(0,2)
via (8,9)(8,7)</pre>
 
 
=={{header|Julia}}==
Modified from the Go version. The task listed word list is offline, so the Debian distribution file "words.txt" was used instead.
<lang julia>using Random
 
const stepdirections = [[1, 0], [0, 1], [1, 1], [1, -1], [-1, 0], [0, -1], [-1, -1], [-1, 1]]
const nrows = 10
const ncols = nrows
const gridsize = nrows * ncols
const minwords = 25
const minwordsize = 3
 
mutable struct LetterGrid
nattempts::Int
nrows::Int
ncols::Int
cells::Matrix{Char}
solutions::Vector{String}
LetterGrid() = new(0, nrows, ncols, fill(' ', nrows, ncols), Vector{String}())
end
 
function wordmatrix(filename)
words = [lowercase(line) for line in readlines(filename)
if match(r"^[a-zA-Z]+$", line) != nothing &&
length(line) >= minwordsize && length(line) <= ncols]
n = 100
for i in 1:n
grid = LetterGrid()
messagelen = placemessage(grid, "Rosetta Code")
target = grid.nrows * grid.ncols - messagelen
cellsfilled = 0
shuffle!(words)
for word in words
cellsfilled += tryplaceword(grid, word)
if cellsfilled == target
if length(grid.solutions) >= minwords
grid.nattempts = i
return grid
else
break
end
end
end
end
throw("Failed to place words after n attempts")
end
 
function placemessage(grid, msg)
msg = uppercase(msg)
msg = replace(msg, r"[^A-Z]" => "")
messagelen = length(msg)
if messagelen > 0 && messagelen < gridsize
p = Int.(floor.(LinRange(messagelen, gridsize, messagelen) .+
(rand(messagelen) .- 0.5) * messagelen / 3)) .- div(messagelen, 3)
foreach(i -> grid.cells[div(p[i], nrows) + 1, p[i] % nrows + 1] = msg[i], 1:length(p))
return messagelen
end
return 0
end
 
function tryplaceword(grid, word)
for dir in shuffle(stepdirections)
for pos in shuffle(1:length(grid.cells))
lettersplaced = trylocation(grid, word, dir, pos)
if lettersplaced > 0
return lettersplaced
end
end
end
return 0
end
 
function trylocation(grid, word, dir, pos)
r, c = divrem(pos, nrows) .+ [1, 1]
positions = [[r, c] .+ (dir .* i) for i in 1:length(word)]
if !all(x -> 0 < x[1] <= nrows && 0 < x[2] <= ncols, positions)
return 0
end
for (i, p) in enumerate(positions)
letter = grid.cells[p[1],p[2]]
if letter != ' ' && letter != word[i]
return 0
end
end
lettersplaced = 0
for (i, p) in enumerate(positions)
if grid.cells[p[1], p[2]] == ' '
lettersplaced += 1
grid.cells[p[1],p[2]] = word[i]
end
end
if lettersplaced > 0
push!(grid.solutions, lpad(word, 10) * " $(positions[1]) to $(positions[end])")
end
return lettersplaced
end
 
function printresult(grid)
if grid.nattempts == 0
println("No grid to display: no solution found.")
return
end
size = length(grid.solutions)
println("Attempts: ", grid.nattempts)
println("Number of words: ", size)
println("\n 0 1 2 3 4 5 6 7 8 9")
for r in 1:nrows
print("\n$r ")
for c in 1:ncols
print(" $(grid.cells[r, c]) ")
end
end
println()
for i in 1:2:size
println("$(grid.solutions[i]) $(i < size ? grid.solutions[i+1] : "")")
end
end
 
printresult(wordmatrix("words.txt"))
</lang>{{output}}<pre>
1 e s o d u m a h d R
2 z n o u g h t y O g
3 i b a p o s c S o r
4 r u f m e E s e g e
5 y t T a y r r r r i
6 h T u r d o k o o g
7 p A u t p l h y s e
8 r C b s e l e a O s
9 o o i a l e g m r D
10 p b o b e t s E e b
bispore [10, 2] to [4, 8] dumah [1, 4] to [1, 8]
porphyrize [10, 1] to [1, 1] melda [9, 8] to [5, 4]
bobet [10, 2] to [10, 6] greiges [2, 10] to [8, 10]
hoyman [7, 7] to [2, 2] noughty [2, 2] to [2, 8]
sorgo [7, 9] to [3, 9] tutees [5, 2] to [10, 7]
perky [3, 4] to [7, 8] osc [3, 5] to [3, 7]
tbo [7, 4] to [9, 2] tub [5, 2] to [3, 2]
gela [9, 7] to [9, 4] arb [8, 8] to [10, 10]
ors [6, 9] to [4, 7] hll [7, 7] to [9, 5]
doko [6, 5] to [6, 8] udos [1, 5] to [1, 2]
tra [7, 4] to [5, 4] utp [7, 3] to [7, 5]
bfa [3, 2] to [5, 4] cyd [3, 7] to [1, 9]
eme [8, 7] to [10, 9]
 
=={{header|Kotlin}}==
4,105

edits