In each play of Extreme Wordle you are to guess a secret five-letter English word. After you make a guess with your five-letter word you are told:
- which of your letters (if any) appear somewhere in the secret word (but that they are not in its sequence position)
- which of your letters (if any) appear in the secret word (but are in their sequence positions)
Duplicate letters are permitted: If a guess has two "t"s (for instance), then the result returned must have two "t"s.
For instance, suppose you guess "AUGHT" and the response is that "A" and "U" do not appear at all in the secret word, that "G" and "H" appear (but not in their current sequence positions), and that "T" appears in its corresponding position (i.e., last of five characters).
Given this clue you are thus to guess the secret word.
In this case, then, you seek a word that contains "G" and "H" (but not in the third and fourth positions, respectively), and contains "T" in the final position.
The answer in this example is: "GHOST"
I'd like to write code to solve Extreme Wordle, and I managed, but my code is extremely ugly. I have a feeling there is a very elegant solution, perhaps using Regular Expressions.
Here the guess is a string such as "AUGHT", and the matches is a list of five integers, where 0 represents "does not appear in the secret word", 1 represents "appears in the secret word but not in the sequence position in the guess word", and 2 represents "appears in the secret word in the sequence position of the guess word." Thus for the "AUGHT" example, matches = {0,0,1,1,2}.
An initial approach for the example case would be related to this form:
extremeWordle[guess_String, matches_List] :=
Select[DictionaryLookup[___],
(ContainsAll[Characters[#],
Characters[guess][[Flatten@Position[matches, 1]]]] &&
ContainsNone[Characters[#],
Characters[guess][[Flatten@Position[matches, 0]]]] &&
Characters[#][[Flatten@Position[matches, 2]]] ==
Characters[guess][[Flatten@Position[matches, 2]]] &&
Characters[#][[Flatten@Position[matches, 1]]] !=
Characters[guess][[Flatten@Position[matches, 1]]] &&
StringLength[#] == 5) &]
but this gives the solution along with an error message:
extremeWordle["aught", {0, 0, 1, 1, 2}]
(* {ghost} *)
I am sure there is an extremely elegant (few characters) solution that is much better.
Here are some examples for you to test your code:
- "cawed", {2,0,1,0,2}
- "coats", {1,1,1,1,0}
- "wefts", {1,0,2,0,0}
- "group", {0,1,0,2,2}
- "ozone", {0,2,0,0,2}
StringLength[#]==5the first of your five conditions. In this way, the other four conditions are only checked if the word is known to have length $5$. – user293787 Aug 22 '22 at 05:05