2

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:

  1. which of your letters (if any) appear somewhere in the secret word (but that they are not in its sequence position)
  2. 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}
David G. Stork
  • 41,180
  • 3
  • 34
  • 96

1 Answers1

3

Here is a variant of OPs code with the following small modifications:

  • The result of Position is used with Extract.
  • An abbreviation aux is used to avoid some repetition.
  • Only words of length 5 are looked up in the dictionary to begin with.
extremeWordle[guess_String,matches_List]:=With[{
   aux=Extract[Characters[#1],Position[matches,#2]]&},
     Select[DictionaryLookup[RegularExpression["....."]],(
        ContainsAll[Characters[#],aux[guess,1]]&&
        ContainsNone[Characters[#],aux[guess,0]]&&
        aux[#,2]===aux[guess,2]&&
        (And@@MapThread[(#1=!=#2)&,{aux[#,1],aux[guess,1]}])
)&]];
user293787
  • 11,833
  • 10
  • 28
  • Yes... far more elegant. Thanks. That suffices. I don't see any way to make the code terser. ($+1, \checkmark$) – David G. Stork Aug 22 '22 at 05:15
  • Actually, your code has an error on corner cases: "digit",{0,1,1,1,0} gives "solutions" that have the wrong number of "i"s. Can you fix your code? – David G. Stork Aug 22 '22 at 05:41
  • Is the new version better? I am not sure what you mean by "wrong number". For example, your "digit" example will give "beige" as one solution, which I think is ok, even though it has only one "i". – user293787 Aug 22 '22 at 05:55
  • I think the problem demands that if the guess has two "i"s that appear in the guess word then the candidate solution MUST have two "i"s. For example, your "digit" -> "beige" is not a solution. It should give just "icing" (two "i"s). Clear? [I'll clarify the problem.] – David G. Stork Aug 22 '22 at 06:24
  • The rules are still not entirely clear to me, and unfortunately I do not have time now. In this NYT document it says that "A repeated letter in a guessed word will be colored only as many times as it appears in the solution". I think this means that if the secret word is "beige", and the guess is "digit", then the code would be {0,1,1,0,0}. The fact that the second i becomes a 0 is not clear for me from your question, even after your edit. – user293787 Aug 22 '22 at 09:20
  • What I still do not know: If the secret word is "begin" and the guess is "digit", would the code be {0,1,2,2,0} or {0,0,2,2,0}? Once the rules are clear, I think a clean program would have two functions. One function code[secret_,guess_]:=... that computes the 012-code. A second function that would simply be extremeWordle[guess_String,matches_List] := Select[DictionaryLookup[RegularExpression["....."]],(code[#,guess]===matches)&];. – user293787 Aug 22 '22 at 09:21
  • I'm not sure, so I think we must define the game clearer than even the posers state. Let's go with: {0,1,2,2,0} for your "begin"/"digit" case. (Also... thanks for taking this cool problem seriously... even after getting an acceptance!) – David G. Stork Aug 22 '22 at 16:03