How to resolve the algorithm Wordle comparison step by step in the Raku programming language
How to resolve the algorithm Wordle comparison step by step in the Raku 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 Raku programming language
Source code in the raku programming language
# 20220216 Raku programming solution
sub wordle (\answer,\guess where [==] (answer,guess)».chars ) {
my ($aSet, $gSet, @return) = (answer,guess)».&{ (set .comb.pairs).SetHash }
(my \intersection = $aSet ∩ $gSet).keys».&{ @return[.key] = 'green' }
($aSet,$gSet)».&{ $_ ∖= intersection } # purge common subset
for $gSet.keys.sort -> \trial { # pair
@return[trial.key] = 'grey';
for $aSet.keys -> \actual { # pair
if [eq] (trial,actual)».value {
@return[trial.key] = 'yellow';
$aSet{actual}:delete;
last
}
my @NFD = (trial,actual).map: { .value.NFD }
if [ne] @NFD and [==] @NFD».first {
@return[trial.key] = 'azure';
$aSet{actual}:delete;
last
}
}
}
@return
}
say .[0]~' vs '~.[1]~"\t"~ wordle .[0],.[1] for (
, , , , ,
, , , );
You may also check:How to resolve the algorithm Singly-linked list/Element definition step by step in the Perl programming language
You may also check:How to resolve the algorithm Circles of given radius through two points step by step in the Seed7 programming language
You may also check:How to resolve the algorithm Loops/With multiple ranges step by step in the XPL0 programming language
You may also check:How to resolve the algorithm String prepend step by step in the M2000 Interpreter programming language
You may also check:How to resolve the algorithm Proper divisors step by step in the Arturo programming language