Suppose I select all 3 char words from the Mathematica dictionary:
all3 = Characters /@ Select[DictionaryLookup[], StringLength[#] == 3 &];
and I want to form full scrabble-like sets, like:
A B E
R A Y
E R E
Where the words can be read horizontally and vertically.
Clearly, the sets can be found with recursion and backtracking. But:
1) Is there a way to solve it using patterns?
2) For which dimensions are there valid solutions?
Edit
I wrote the question for DictionaryLookup[]
just because it's a reasonable sized database of variable length records. My real problem is not related to Dictionary lookups but to a certain kind of loom patterns.
I am not sure if you would consider the following approach pattern based -- but it works, and it can conceivably be extended to many dimensions, although with the
all3
dataset, it would probably konk out rather early...The idea is to start with a blank crossword:
and then recursively do the following: For a given pattern, look at the rows in turn and (after filling out any with exactly one completion) expand the pattern on the row with the fewest number of matches:
For a given candidate pattern, try out the completion along both dimensions and keep the one that yield the fewest:
I do the recursion breadth first to be able to use Union, but depth first might be necessary for bigger problems. Performance is so-so: 8 laptop minutes to find the 116568 matches in the example problem:
In principle, it should be possible to recurse this into higher dimensions, i.e. using the crosswords list instead of the wordlist for dimension 3. If the time to match a pattern against a list is linear in the list-length, this would be quite slow with a 100000+ sized wordlist...