/* Wordle comparison (Rosetta code) in Picat. http://rosettacode.org/wiki/Wordle_comparison """ 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. """ This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import util. main => go. /* My preferred representation of textual answer: * correct pos (gren): Uppercase * correct char (yellow): lowercase + "*" * not in word (grey): lowercase */ % {{trans|Go}} go => Pairs = [["ALLOW", "LOLLY"], ["BULLY", "LOLLY"], ["ROBIN", "ALERT"], ["ROBIN", "SONIC"], ["ROBIN", "ROBIN"], ["ROBBY", "OBBYR"], ["ROSETTA", "OSSETAR"]], foreach([Answer,Guess] in Pairs) [Pres,Res] = wordle1(Answer,Guess), Len = Res.len, printf("%w v %w => %w %w\n", Answer, Guess, Pres, Res) end, nl. wordle1(Answer,Guess) = [Presentation,Result] => N = Guess.len, Answer2 = copy_term(Answer), % don't touch Answer if N != Answer.len then print("The words must be of the same length.") else Result = new_list(N,grey), % grey by default Presentation = copy_term(Guess).map(to_lowercase), foreach(I in 1..N, Guess[I] == Answer2[I]) Answer2[I] := "", Result[I] := green, Presentation[I] := [to_uppercase(Guess[I])] ++ " " end, foreach(I in 1..N, Ix = find_first_of(Answer2,Guess[I]), Ix > 0) Answer2[Ix] := "", Result[I] := yellow, Presentation[I] := [to_lowercase(Guess[I])] ++ "*" end end.