How to resolve the algorithm Wordle comparison step by step in the jq programming language

Published on 12 May 2024 09:40 PM
#Jq

How to resolve the algorithm Wordle comparison step by step in the jq programming language

Table of Contents

Problem Statement

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 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.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Wordle comparison step by step in the jq programming language

Source code in the jq programming language

def colors: ["grey", "yellow", "green"];
 
def wordle($answer; $guess):
  ($guess|length) as $n
  | if ($answer|length) != $n then "The words must be of the same length." | error
    else { answer: (answer | explode),
           guess:  (guess  | explode),
	   result: [range(0;$n)|0] }
    | reduce range(0; $n) as $i (.;
        if .guess[$i] == .answer[$i]
        then .answer[$i] = 0
        | .result[$i] = 2
        else .
	end )
    | reduce range(0; $n) as $i (.;
        .guess[$i] as $g
        | (.answer | index($g) ) as $ix
        | if $ix
          then .answer[$ix] = 0
          | .result[$i] = 1
          else .
	  end )
    | .result
    end ;
 
def pairs:
    ["ALLOW", "LOLLY"],
    ["BULLY", "LOLLY"],
    ["ROBIN", "ALERT"],
    ["ROBIN", "SONIC"],
    ["ROBIN", "ROBIN"]
;

pairs
| wordle(.[0]; .[1]) as $res
| ($res | map(colors[.])) as $res2
| "\(.[0]) v \(.[1]) => \($res) => \($res2)"

  

You may also check:How to resolve the algorithm GUI component interaction step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm Polymorphism step by step in the OxygenBasic programming language
You may also check:How to resolve the algorithm Suffixation of decimal numbers step by step in the REXX programming language
You may also check:How to resolve the algorithm Parametric polymorphism step by step in the Ursala programming language
You may also check:How to resolve the algorithm FizzBuzz step by step in the Yabasic programming language