I was intrigued so I played around with this a bit. I’m hardcoding one case here because it was faster to set up.
Basically the idea was to create an “array” from CartesianIndices. Then use mappedarray to make one word out of each index. Then in that array I can just search for the word-tuple and everything is inferred. I mean this particular problem you can probably also just solve with loops, but I thought it was kind of cool to set it up this way, and I learned a couple new things as well
using MappedArrays
using BenchmarkTools
get_word(idx, chars) = map(i -> chars[i], Tuple(idx)) # word is just a tuple of chars
function wordsearch()
chars = ('a', 'b', 'c', 'd', 'e', 'f')
wordlength = 6
indices = CartesianIndices(ntuple(x -> length(chars), wordlength))
lazy_arr = mappedarray(x -> get_word(x, chars), indices)
word = ('f', 'b', 'c', 'd', 'a', 'd')
findfirst(==(word), lazy_arr)
end
@btime wordsearch()
89.885 μs (0 allocations: 0 bytes)
CartesianIndex(6, 2, 3, 4, 1, 4)