Wordle comparison: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎{{header|Julia}}: Changed to match revised Wren solution.)
m (→‎{{header|Phix}}: two loops)
Line 66: Line 66:


=={{header|Phix}}==
=={{header|Phix}}==
<small>Aside: You may be mildly surprised to see the for loop limit being clobbered like this, but you ''cannot'' change the limit mid-loop in Phix (an explicit exit would be far clearer) and hence you ''can'' do this.</small>
<small>Aside: You may be mildly surprised to see the 2nd for loop limit being clobbered like this, but you ''cannot'' change the limit mid-loop in Phix (an explicit exit would be far clearer) and hence you ''can'' do this.</small>
<!--<lang Phix>(phixonline)-->
<!--<lang Phix>(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
Line 74: Line 74:
<span style="color: #004080;">sequence</span> <span style="color: #000000;">result</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">result</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">guess</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">answer</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">guess</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]=</span><span style="color: #000000;">answer</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">then</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">n</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">answer</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">'\0'</span>
<span style="color: #000000;">answer</span><span style="color: #0000FF;">[</span><span style="color: #000000;">n</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">'\0'</span>
<span style="color: #000000;">result</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">2</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">result</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">+(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">==</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">guess</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]!=</span><span style="color: #008000;">'\0'</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">guess</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">answer</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">n</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">answer</span><span style="color: #0000FF;">[</span><span style="color: #000000;">n</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">'\0'</span>
<span style="color: #000000;">result</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
Line 85: Line 93:
<span style="color: #008080;">constant</span> <span style="color: #000000;">tests</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span><span style="color: #008000;">"ALLOW"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"LOLLY"</span><span style="color: #0000FF;">},</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">tests</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span><span style="color: #008000;">"ALLOW"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"LOLLY"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"BULLY"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"LOLLY"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"BULLY"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"LOLLY"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"BULLY"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"lOLLY"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"ROBIN"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"ALERT"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"ROBIN"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"ALERT"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"ROBIN"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"SONIC"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"ROBIN"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"SONIC"</span><span style="color: #0000FF;">},</span>
Line 98: Line 105:
<!--</lang>-->
<!--</lang>-->
{{out}}
{{out}}
lastminute dot com: erm, what is the correct answer for bully vs lolly? see talk page, I will need to update this later.
<pre>
<pre>
ALLOW v LOLLY => {1,1,2,0,0} => {"yellow","yellow","green","grey","grey"}
ALLOW v LOLLY => {1,1,2,0,0} => {"yellow","yellow","green","grey","grey"}
BULLY v LOLLY => {1,0,1,0,2} => {"yellow","grey","yellow","grey","green"}
BULLY v LOLLY => {1,0,1,0,2} => {"grey","grey","green","green","green"}
BULLY v lOLLY => {0,0,2,2,2} => {"grey","grey","green","green","green"}
ROBIN v ALERT => {0,0,0,1,0} => {"grey","grey","grey","yellow","grey"}
ROBIN v ALERT => {0,0,0,1,0} => {"grey","grey","grey","yellow","grey"}
ROBIN v SONIC => {0,2,1,2,0} => {"grey","green","yellow","green","grey"}
ROBIN v SONIC => {0,2,1,2,0} => {"grey","green","yellow","green","grey"}

Revision as of 12:15, 16 February 2022

Wordle comparison is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Rationale

While similar to both Bulls and cows and Mastermind, Wordle is a notable variation, having experienced a viral surge in popularity, and reverse engineering the game or creating variants has become a popular programming exercise. However, a sampling of the "code a Wordle clone" videos on YouTube shows that seven of the eight reviewed had a serious flaw in the way that they assigned colours to the letters of a guessed word. This aspect of the game is described here: en.wikipedia.org/wiki/Wordle#Gameplay

After every guess, each letter is marked as either green, yellow or gray: green indicates that letter is correct and in the correct position, yellow means it is in the answer but not in the right position, while gray indicates it is not in the answer at all. Multiple instances of the same letter in a guess, such as the "o"s in "robot", will be colored green or yellow only if the letter also appears multiple times in the answer; otherwise, excess repeating letters will be colored gray.

Task

Create a function or procedure that takes two strings; the answer string, and the guess string, and returns a string, list, dynamic array or other ordered sequence indicating how each letter should be marked as per the description above. (e.g. "green", "yellow", or "grey", or, equivalently, the integers 2, 1, or 0 or suchlike.)

You can assume that both the answer string and the guess string are the same length, and contain only printable characters/code points in the ASCII/UniCode Range ! to ~ (hex 20 to 7F) and that case is significant. (The original game only uses strings of 5 characters, all alphabetic letters, all in the same case, but this allows for most existing variants of the game.)

Provide test data and show the output here.

The test data should include the answer string ALLOW and the guess string LOLLY, and the result should be (yellow, yellow, green, grey, grey) or equivalent.


Julia

Translation of: Wren

<lang julia>const colors = ["grey", "yellow", "green"]

function wordle(answer, guess)

   n = length(guess)
   length(answer) != n && error("The words must be of the same length.")
   answervector, result = collect(answer), zeros(Int, n)
   for i in 1:n
       if guess[i] == answervector[i]
           answervector[i] = '\0'
           result[i] = 2
       end
   end
   for i in 1:n
       c = guess[i]
       ix = findfirst(isequal(c), answervector)
       if ix != nothing
           answervector[ix] = '\0'
           result[i] = 1
       end
   end
   return result

end

const testpairs = [

   ["ALLOW", "LOLLY"],
   ["BULLY", "LOLLY"],
   ["BULLY", "lOLLY"],
   ["ROBIN", "ALERT"],
   ["ROBIN", "SONIC"],
   ["ROBIN", "ROBIN"]

] for (pair0, pair1) in testpairs

   res  = wordle(pair0, pair1)
   res2 = [colors[i + 1] for i in res]
   println("$pair0 v $pair1 => $res => $res2")

end</lang>

Output:
ALLOW v LOLLY => [1, 1, 2, 0, 0] => ["yellow", "yellow", "green", "grey", "grey"]
BULLY v LOLLY => [0, 0, 2, 2, 2] => ["grey", "grey", "green", "green", "green"]
BULLY v lOLLY => [0, 0, 2, 2, 2] => ["grey", "grey", "green", "green", "green"]
ROBIN v ALERT => [0, 0, 0, 1, 0] => ["grey", "grey", "grey", "yellow", "grey"]
ROBIN v SONIC => [0, 2, 1, 2, 0] => ["grey", "green", "yellow", "green", "grey"]
ROBIN v ROBIN => [2, 2, 2, 2, 2] => ["green", "green", "green", "green", "green"]

Phix

Aside: You may be mildly surprised to see the 2nd for loop limit being clobbered like this, but you cannot change the limit mid-loop in Phix (an explicit exit would be far clearer) and hence you can do this.

with javascript_semantics
function wordle(string answer, guess)
    integer n = length(guess)
    assert(n == length(answer),"words must be same length")
    sequence result = repeat(0,n)
    for i=1 to n do
        if guess[i]=answer[i] then
            answer[i] = '\0'
            result[i] = 2
        end if
    end for
    for i=1 to n do
        if guess[i]!='\0' then
            n = find(guess[i],answer)
            if n then
                answer[n] = '\0'
                result[i] = 1
            end if
        end if
    end for
    return result
end function
 
constant tests = {{"ALLOW", "LOLLY"},
                  {"BULLY", "LOLLY"},
                  {"ROBIN", "ALERT"},
                  {"ROBIN", "SONIC"},
                  {"ROBIN", "ROBIN"}},
         colours = {"grey", "yellow", "green"}
for i=1 to length(tests) do
    string {answer,guess} = tests[i]
    sequence res = wordle(answer,guess),
             rac = extract(colours,sq_add(res,1))
    printf(1,"%s v %s => %v => %v\n",{answer,guess,res,rac})
end for
Output:
ALLOW v LOLLY => {1,1,2,0,0} => {"yellow","yellow","green","grey","grey"}
BULLY v LOLLY => {1,0,1,0,2} => {"grey","grey","green","green","green"}
ROBIN v ALERT => {0,0,0,1,0} => {"grey","grey","grey","yellow","grey"}
ROBIN v SONIC => {0,2,1,2,0} => {"grey","green","yellow","green","grey"}
ROBIN v ROBIN => {2,2,2,2,2} => {"green","green","green","green","green"}

Quackery

<lang Quackery> [ tuck witheach

     [ over i^ peek = if
        [ 2 rot i^ poke swap
          0 swap i^ poke ] ]
   witheach 
     [ over find 
       2dup swap found iff 
         [ 1 unrot poke ] 
       else drop ]
   [] swap witheach 
     [ dup 3 < * join ] ]    is wordle-compare ( $ $ --> [ ) 

 $ "ALLOW" $ "LOLLY" wordle-compare echo cr 
 $ "ROBIN" $ "ALERT" wordle-compare echo cr
 $ "ROBIN" $ "SONIC" wordle-compare echo cr
 $ "ROBIN" $ "ROBIN" wordle-compare echo cr

</lang>

Output:

2 is equivalent to green, 1 is equivalent to yellow, 1 is equivalent to grey

[ 1 1 2 0 0 ]
[ 0 0 0 1 0 ]
[ 0 2 1 2 0 ]
[ 2 2 2 2 2 ]

Wren

<lang ecmascript>var colors = ["grey", "yellow", "green"]

var wordle = Fn.new { |answer, guess|

   var n = guess.count
   if (answer.count != n) Fiber.abort("The words must be of the same length.")
   answer = answer.toList
   var result = List.filled(n, 0)
   for (i in 0...n) {
       if (guess[i] == answer[i]) {
           answer[i] = "\0"
           result[i] = 2
       }
   }
   for (i in 0...n) {
       var ix = answer.indexOf(guess[i])
       if (ix >= 0) {
           answer[ix] = "\0"
           result[i] = 1
       }
   }
   return result

}

var pairs = [

   ["ALLOW", "LOLLY"],
   ["BULLY", "LOLLY"],
   ["BULLY", "lOLLY"],
   ["ROBIN", "ALERT"],
   ["ROBIN", "SONIC"],
   ["ROBIN", "ROBIN"]

] for (pair in pairs) {

   var res  = wordle.call(pair[0], pair[1])
   var res2 = res.map { |i| colors[i] }.toList
   System.print("%(pair[0]) v %(pair[1]) => %(res) => %(res2)")

}</lang>

Output:
ALLOW v LOLLY => [1, 1, 2, 0, 0] => [yellow, yellow, green, grey, grey]
BULLY v LOLLY => [0, 0, 2, 2, 2] => [grey, grey, green, green, green]
BULLY v lOLLY => [0, 0, 2, 2, 2] => [grey, grey, green, green, green]
ROBIN v ALERT => [0, 0, 0, 1, 0] => [grey, grey, grey, yellow, grey]
ROBIN v SONIC => [0, 2, 1, 2, 0] => [grey, green, yellow, green, grey]
ROBIN v ROBIN => [2, 2, 2, 2, 2] => [green, green, green, green, green]