Wordle comparison: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(11 intermediate revisions by 5 users not shown)
Line 1:
{{Draft task}}
 
;Rationale:
Line 156:
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 }
</pre>
 
=={{header|C#}}==
{{works with|.NET 8}}
{{trans|Java}}
<syntaxhighlight lang="C#">
using System;
using System.Collections.Generic;
using System.Linq;
 
public class WordleComparison
{
public static void Main(string[] args)
{
List<TwoWords> pairs = new List<TwoWords>
{
new TwoWords("ALLOW", "LOLLY"),
new TwoWords("ROBIN", "SONIC"),
new TwoWords("CHANT", "LATTE"),
new TwoWords("We're", "She's"),
new TwoWords("NOMAD", "MAMMA")
};
 
foreach (var pair in pairs)
{
Console.WriteLine(pair.Answer + " v " + pair.Guess + " -> " + string.Join(", ", Wordle(pair.Answer, pair.Guess)));
}
}
 
private static List<Colour> Wordle(string answer, string guess)
{
if (answer.Length != guess.Length)
{
throw new ArgumentException("The two words must be of the same length.");
}
 
var result = Enumerable.Repeat(Colour.GREY, guess.Length).ToList();
var answerCopy = answer;
 
for (int i = 0; i < guess.Length; i++)
{
if (answer[i] == guess[i])
{
answerCopy = answerCopy.Remove(i, 1).Insert(i, "\0");
result[i] = Colour.GREEN;
}
}
 
for (int i = 0; i < guess.Length; i++)
{
int index = answerCopy.IndexOf(guess[i]);
if (index >= 0 && result[i] != Colour.GREEN)
{
answerCopy = answerCopy.Remove(index, 1).Insert(index, "\0");
result[i] = Colour.YELLOW;
}
}
 
return result;
}
 
private enum Colour { GREEN, GREY, YELLOW }
 
private record TwoWords(string Answer, string Guess);
}
</syntaxhighlight>
{{out}}
<pre>
ALLOW v LOLLY -> YELLOW, YELLOW, GREEN, GREY, GREY
ROBIN v SONIC -> GREY, GREEN, YELLOW, GREEN, GREY
CHANT v LATTE -> GREY, YELLOW, YELLOW, GREY, GREY
We're v She's -> GREY, GREY, YELLOW, YELLOW, GREY
NOMAD v MAMMA -> GREY, YELLOW, GREEN, GREY, GREY
</pre>
 
=={{header|C++}}==
<syntaxhighlight lang="c++">
#include <cstdint>
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>
 
enum Colour { GREEN, GREY, YELLOW };
 
const char NIL = '\0';
 
struct Two_Words {
std::string answer;
std::string guess;
};
 
std::string to_string(const Colour& colour) {
std::string result;
switch ( colour ) {
case Colour::GREEN : result = "Green"; break;
case Colour::GREY : result = "Grey"; break;
case Colour::YELLOW : result = "Yellow"; break;
}
return result;
}
 
std::vector<Colour> wordle(const std::string& answer, const std::string& guess) {
const uint32_t guess_length = guess.length();
if ( answer.length() != guess_length ) {
throw std::invalid_argument("The two words must have the same length.");
}
 
std::string answerCopy = answer;
std::vector<Colour> result(guess_length, Colour::GREY);
for ( uint32_t i = 0; i < guess_length; ++i ) {
if ( answer[i] == guess[i] ) {
answerCopy[i] = NIL;
result[i] = Colour::GREEN;
}
}
 
for ( uint32_t i = 0; i < guess_length; ++i ) {
std::string::size_type index = answerCopy.find(guess[i]);
if ( index != std::string::npos ) {
answerCopy[index] = NIL;
result[i] = Colour::YELLOW;
}
}
return result;
}
 
int main() {
const std::vector<Two_Words> pairs = { Two_Words("ALLOW", "LOLLY"), Two_Words("ROBIN", "SONIC"),
Two_Words("CHANT", "LATTE"), Two_Words("We're", "She's"), Two_Words("NOMAD", "MAMMA") };
 
for ( const Two_Words& pair : pairs ) {
std::vector<Colour> colours = wordle(pair.answer, pair.guess);
std::cout << pair.answer << " v " << pair.guess << " -> [";
for ( uint32_t i = 0; i < pair.answer.length() - 1; ++i ) {
std::cout << to_string(colours[i]) << ", ";
}
std::cout << to_string(colours.back()) << "]" << std::endl;
}
}
</syntaxhighlight>
{{ out }}
<pre>
ALLOW v LOLLY -> [Yellow, Yellow, Green, Grey, Grey]
ROBIN v SONIC -> [Grey, Green, Yellow, Green, Grey]
CHANT v LATTE -> [Grey, Yellow, Yellow, Grey, Grey]
We're v She's -> [Grey, Grey, Yellow, Yellow, Grey]
NOMAD v MAMMA -> [Grey, Yellow, Green, Grey, Grey]
</pre>
 
Line 284 ⟶ 432:
</pre>
 
=={{header|Forth}}==
<syntaxhighlight lang="forth">
: color! ( c i -- ) tuck pad + c! here + 0 swap c! ;
 
: wordle ( a1 u1 a2 u2 -- a3 u3 )
2swap here swap move pad over erase dup 0 do
over i + c@ here i + c@ = if 2 i color! then
loop dup 0 do
2dup here swap rot i + 1 search nip nip if 1 i color! then
loop nip pad swap ;
 
:noname
create s" grey" , , s" yellow" , , s" green" , ,
does> swap 2* cells + 2@ type ; execute color.
 
: .wordle ( a1 u1 a2 u2 -- )
2over type ." v " 2dup type ." => " wordle
over + swap do i c@ color. space loop cr ;
 
:noname
0 s" ROBIN" 2dup 2dup s" SONIC" 2over s" ALERT"
s" BULLY" s" LOLLY" s" ALLOW" 2over
begin ?dup while .wordle repeat ; execute
</syntaxhighlight>
{{out}}
<pre>
ALLOW v LOLLY => yellow yellow green grey grey
BULLY v LOLLY => grey grey green green green
ROBIN v ALERT => grey grey grey yellow grey
ROBIN v SONIC => grey green yellow green grey
ROBIN v ROBIN => green green green green green
</pre>
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">Function wordle(Byval respuesta As String, Byval supuesto As String) As String
Line 354 ⟶ 533:
BBBBBAA: Expected 5 character guess.
BBAABBB: Expected 5 character target.</pre>
 
 
=={{header|FutureBasic}}==
This compact FutureBasic function fillsreturns ana byte array of(as colora objectspstring) whichwith can2, be1, applied0, directlyfor tomatched, whatever output is chosenmismatched, as demonstrated in the 'show' functionunmatched. It is useful for games up to 15 chars wide, and is case sensitive as specified.
<syntaxhighlight lang="futurebasic">
short x = 80, y = 20
 
clear local fn colorString( w1 as str15, w2 as str15 ) as str15
short x, y = -29
str255 n : str15 c : c[0] = w2[0] : short r
 
for r = 1 to w1[0]
clear local fn wordleCompare( wordle as str15, guess as str15, cArray(15) as colorRef )
if w2[r] = w1[r] then c[r] = 2 else n[w1[r]]++
/* Returns colors in passed array "cArray()". */
byte count(255)
short r
for r = 1 to wordle[0]
if guess[r] == wordle[r] then cArray(r) = fn colorGreen : continue
count(wordle[r])++ : cArray(r) = fn colorGray
next
for r = 1 to w2[0]
if c[r] == 0 then if n[w2[r]] then n[w2[r]]-- : c[r] = 1
for r = 1 to guess[0]
if cArray(r) == fn colorGreen then continue
if count(guess[r]) then count(guess[r])-- : cArray(r) = fn colorYellow
next
end fn = c
</syntaxhighlight>
end fn
This function uses the array to display output mimicking the appearance of WORDLE.
 
<syntaxhighlight lang="futurebasic">
local fn show( wordle as str15, guess as str15 )
mda(0) = {fn ColorDarkGray,fn ColorWithRGB(.7,.6,.3,1),fn ColorWithRGB(.3,.6,.3,1)}
colorref color(15)
void local fn wordleCompare( wordle as str15, guess as str15 )
short r
xstr15 = 130color : y +=short 32r
color = fn wordleComparecolorString( wordle, guess, color(0) ) //get color array
text @"menlo bold", 2714, fn colorgray : print %( 20, y ) wordlecolorLightGray
print %( 20, y ) wordle : text ,,fn colorBlackcolorWhite
for r = 1 to guess[0]
rect fill ( x, y + 8, 2824, 2824 ), colormda_object(r) //apply color[r] from array to square)
print %( x + 7.5, y + 1 ) chr$( guess[r] ); : x += 2928
next
x = 80 : y += 28
end fn
 
window 1, @"FB Wordle Compare in FutureBasic", ( 0, 0,310 265,350 290 )
WindowSetBackgroundColor( 1, fn Colorblack )
fn show( "ALLOW", "LOLLY" )
fn showwordleCompare( "CHANTALLOW", "LATTELOLLY" )
fn showwordleCompare( "ROBINCHANT", "SONICLATTE" )
fn showwordleCompare( "ROBIN", "ROBINSONIC" )
fn showwordleCompare( "ROBINPROUD", "ALERTLEAST" )
fn showwordleCompare( "BULLYSTEAL", "LOLLYLEAST" )
fn showwordleCompare( "STEALLEAST", "LEAST" )
fn showwordleCompare( "CHOIRFULLY", "LEASTLABEL" )
fn showwordleCompare( "SILLYWe're", "LABELShe's" )
fn showwordleCompare( "SwissLONGER", "sissySTRING" )
 
handleevents
</syntaxhighlight>
{{out}}
[[File:FutureBasicWordle OutputComparison forin Wordle ComparisonFutureBasic.png]]
 
=={{header|Go}}==
Line 602 ⟶ 774:
<syntaxhighlight lang="j"> ('allow' wrdcmp 'lolly')&{&.;: 'gray yellow green'
yellow yellow green gray gray</syntaxhighlight>
 
=={{header|Java}}==
<syntaxhighlight lang="java">
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
 
public final class WordleComparison {
 
public static void main(String[] args) {
List<TwoWords> pairs = List.of( new TwoWords("ALLOW", "LOLLY"), new TwoWords("ROBIN", "SONIC"),
new TwoWords("CHANT", "LATTE"), new TwoWords("We're", "She's"), new TwoWords("NOMAD", "MAMMA") );
for ( TwoWords pair : pairs ) {
System.out.println(pair.answer + " v " + pair.guess + " -> " + wordle(pair.answer, pair.guess));
}
}
private static List<Colour> wordle(String answer, String guess) {
final int guessLength = guess.length();
if ( answer.length() != guessLength ) {
throw new AssertionError("The two words must be of the same length.");
}
String answerCopy = answer;
List<Colour> result = Stream.generate( () -> Colour.GREY ).limit(guessLength).collect(Collectors.toList());
for ( int i = 0; i < guessLength; i++ ) {
if ( answer.charAt(i) == guess.charAt(i) ) {
answerCopy = answerCopy.substring(0, i) + NULL + answerCopy.substring(i + 1);
result.set(i, Colour.GREEN);
}
}
for ( int i = 0; i < guessLength; i++ ) {
int index = answerCopy.indexOf(guess.charAt(i));
if ( index >= 0 ) {
answerCopy = answerCopy.substring(0, index) + NULL + answerCopy.substring(index + 1);
result.set(i, Colour.YELLOW);
}
}
return result;
}
private enum Colour { GREEN, GREY, YELLOW }
private static record TwoWords(String answer, String guess) {}
private static final char NULL = '\0';
 
}
</syntaxhighlight>
{{ out }}
<pre>
ALLOW v LOLLY -> [YELLOW, YELLOW, GREEN, GREY, GREY]
ROBIN v SONIC -> [GREY, GREEN, YELLOW, GREEN, GREY]
CHANT v LATTE -> [GREY, YELLOW, YELLOW, GREY, GREY]
We're v She's -> [GREY, GREY, YELLOW, YELLOW, GREY]
NOMAD v MAMMA -> [GREY, YELLOW, GREEN, GREY, GREY]
</pre>
 
=={{header|JavaScript}}==
Line 895 ⟶ 1,126:
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"]
</pre>
 
=={{header|Kotlin}}==
{{trans|Java}}
<syntaxhighlight lang="Kotlin">
fun main() {
val pairs = listOf(
TwoWords("ALLOW", "LOLLY"),
TwoWords("ROBIN", "SONIC"),
TwoWords("CHANT", "LATTE"),
TwoWords("We're", "She's"),
TwoWords("NOMAD", "MAMMA")
)
 
for (pair in pairs) {
println("${pair.answer} v ${pair.guess} -> ${wordle(pair.answer, pair.guess)}")
}
}
 
private fun wordle(answer: String, guess: String): List<Colour> {
val guessLength = guess.length
require(answer.length == guessLength) { "The two words must be of the same length." }
 
var answerCopy = answer
val result = MutableList(guessLength) { Colour.GREY }
for (i in guess.indices) {
if (answer[i] == guess[i]) {
answerCopy = answerCopy.substring(0, i) + NULL + answerCopy.substring(i + 1)
result[i] = Colour.GREEN
}
}
 
for (i in guess.indices) {
val index = answerCopy.indexOf(guess[i])
if (index >= 0) {
answerCopy = answerCopy.substring(0, index) + NULL + answerCopy.substring(index + 1)
if (result[i] != Colour.GREEN) {
result[i] = Colour.YELLOW
}
}
}
return result
}
 
private enum class Colour { GREEN, GREY, YELLOW }
 
private data class TwoWords(val answer: String, val guess: String)
 
private const val NULL = '\u0000'
</syntaxhighlight>
{{out}}
<pre>
ALLOW v LOLLY -> [YELLOW, YELLOW, GREEN, GREY, GREY]
ROBIN v SONIC -> [GREY, GREEN, YELLOW, GREEN, GREY]
CHANT v LATTE -> [GREY, YELLOW, YELLOW, GREY, GREY]
We're v She's -> [GREY, GREY, YELLOW, YELLOW, GREY]
NOMAD v MAMMA -> [GREY, YELLOW, GREEN, GREY, GREY]
 
</pre>
 
Line 936 ⟶ 1,225:
ROBIN v SONIC → (grey, green, yellow, green, grey)
ROBIN v ROBIN → (green, green, green, green, green)</pre>
 
=={{header|PARI/GP}}==
{{trans|Julia}}
<syntaxhighlight lang="PARI/GP">
colors = ["grey", "yellow", "green"];
 
wordle(answer, guess) =
{
n = #guess;
if (#answer != n, error("The words must be of the same length."));
answervector = Vecsmall(answer);
guessvector = Vecsmall(guess); \\ Convert guess to a vector of ASCII values
result = vector(n, i, 0);
for (i = 1, n,
if (guessvector[i] == answervector[i],
answervector[i] = 0;
result[i] = 2;
);
);
for (i = 1, n,
c = guessvector[i];
for (j = 1, n,
if (answervector[j] == c,
answervector[j] = 0;
result[i] = 1;
break;
);
);
);
result;
}
 
{
testpairs = [["ALLOW", "LOLLY"], ["BULLY", "LOLLY"], ["ROBIN", "ALERT"], ["ROBIN", "SONIC"], ["ROBIN", "ROBIN"]];
for (i = 1, #testpairs,
pair = testpairs[i];
res = wordle(pair[1], pair[2]);
res2 = vector(#res, j, colors[res[j] + 1]);
print(pair[1] " v " pair[2] " => " res " => " res2);
)
}
</syntaxhighlight>
{{out}}
<pre>
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"]
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"]
 
</pre>
 
=={{header|Perl}}==
Line 1,436 ⟶ 1,776:
BBAABBB vs BBBBBAA green green yellow yellow green yellow yellow
BBAABBB vs AABBBAA yellow yellow yellow yellow green grey grey
</pre>
 
=={{header|Scala}}==
{{trans|Java}}
<syntaxhighlight lang="Scala">
object WordleComparison extends App {
 
case class TwoWords(answer: String, guess: String)
enum Colour extends Enum[Colour] {
case GREEN, GREY, YELLOW
}
 
val pairs = List(TwoWords("ALLOW", "LOLLY"), TwoWords("ROBIN", "SONIC"),
TwoWords("CHANT", "LATTE"), TwoWords("We're", "She's"), TwoWords("NOMAD", "MAMMA"))
 
pairs.foreach(pair => println(s"${pair.answer} v ${pair.guess} -> ${wordle(pair.answer, pair.guess)}"))
 
def wordle(answer: String, guess: String): List[Colour] = {
if (answer.length != guess.length) {
throw new AssertionError("The two words must be of the same length.")
}
 
var answerCopy = answer
var result = List.fill(guess.length)(Colour.GREY)
 
for (i <- guess.indices) {
if (answer(i) == guess(i)) {
answerCopy = answerCopy.updated(i, NULL)
result = result.updated(i, Colour.GREEN)
}
}
 
for (i <- guess.indices) {
val index = answerCopy.indexOf(guess(i))
if (index >= 0 && result(i) != Colour.GREEN) {
answerCopy = answerCopy.updated(index, NULL)
result = result.updated(i, Colour.YELLOW)
}
}
result
}
 
val NULL = '\u0000'
}
</syntaxhighlight>
{{out}}
<pre>
ALLOW v LOLLY -> List(YELLOW, YELLOW, GREEN, GREY, GREY)
ROBIN v SONIC -> List(GREY, GREEN, YELLOW, GREEN, GREY)
CHANT v LATTE -> List(GREY, YELLOW, YELLOW, GREY, GREY)
We're v She's -> List(GREY, GREY, YELLOW, YELLOW, GREY)
NOMAD v MAMMA -> List(GREY, YELLOW, GREEN, GREY, GREY)
 
</pre>
 
Line 1,585 ⟶ 1,978:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">var colors = ["grey", "yellow", "green"]
 
var wordle = Fn.new { |answer, guess|
9,476

edits