Wordle comparison: Difference between revisions

Content added Content deleted
(→‎{{header|Picat}}: Moving 2nd presentation to wordle predicate)
m (syntax highlighting fixup automation)
Line 20: Line 20:
=={{header|C}}==
=={{header|C}}==
{{trans|Wren}}
{{trans|Wren}}
<lang c>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <string.h>
#include <string.h>
Line 74: Line 74:
}
}
return 0;
return 0;
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 86: Line 86:


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<lang freebasic>Function wordle(Byval respuesta As String, Byval supuesto As String) As String
<syntaxhighlight lang="freebasic">Function wordle(Byval respuesta As String, Byval supuesto As String) As String
Dim As Integer n, i, k
Dim As Integer n, i, k
Dim As String resultado
Dim As String resultado
Line 142: Line 142:
End If
End If
Next i
Next i
Sleep</lang>
Sleep</syntaxhighlight>
{{out}}
{{out}}
<pre>ALLOW v LOLLY => [1, 1, 2, 0, 0] => yellow yellow green grey grey
<pre>ALLOW v LOLLY => [1, 1, 2, 0, 0] => yellow yellow green grey grey
Line 158: Line 158:
=={{header|Go}}==
=={{header|Go}}==
{{trans|Wren}}
{{trans|Wren}}
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 206: Line 206:
fmt.Printf("%s v %s => %v => %v\n", pair[0], pair[1], res, res2)
fmt.Printf("%s v %s => %v => %v\n", pair[0], pair[1], res, res2)
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 218: Line 218:


=={{header|Haskell}}==
=={{header|Haskell}}==
<lang haskell>import Data.Bifunctor (first)
<syntaxhighlight lang="haskell">import Data.Bifunctor (first)
import Data.List (intercalate, mapAccumL)
import Data.List (intercalate, mapAccumL)
import qualified Data.Map.Strict as M
import qualified Data.Map.Strict as M
Line 291: Line 291:
color 2 = "green"
color 2 = "green"
color 1 = "amber"
color 1 = "amber"
color _ = "gray"</lang>
color _ = "gray"</syntaxhighlight>
{{Out}}
{{Out}}
<pre>Target -> Guess -> Scores
<pre>Target -> Guess -> Scores
Line 310: Line 310:
Implementation (brute force):
Implementation (brute force):


<lang J>wrdcmp=: {{
<syntaxhighlight lang="j">wrdcmp=: {{
yw=.gr=. I.x=y
yw=.gr=. I.x=y
key=. '#' gr} x
key=. '#' gr} x
Line 320: Line 320:
end.
end.
2 gr} 1 yw} (#y)#0
2 gr} 1 yw} (#y)#0
}}</lang>
}}</syntaxhighlight>


A bit more efficient (about 3 times faster on task example, which might matter if a few microseconds was important):
A bit more efficient (about 3 times faster on task example, which might matter if a few microseconds was important):


<lang J>wrdcmp=: {{
<syntaxhighlight lang="j">wrdcmp=: {{
yw=. ;(] , ({.~1<.#)@-.)&.>/(<@I.y=/~x#~y~:x),<I.x=y
yw=. ;(] , ({.~1<.#)@-.)&.>/(<@I.y=/~x#~y~:x),<I.x=y
2 (I.x=y)} 1 yw} (#y)#0
2 (I.x=y)} 1 yw} (#y)#0
Line 335: Line 335:
assert 2 2 2 2 2-: 'robin' wrdcmp 'robin'
assert 2 2 2 2 2-: 'robin' wrdcmp 'robin'
assert 0 0 2 1 0-: 'mamma' wrdcmp 'nomad'
assert 0 0 2 1 0-: 'mamma' wrdcmp 'nomad'
assert 0 1 2 0 0-: 'nomad' wrdcmp 'mamma'</lang>
assert 0 1 2 0 0-: 'nomad' wrdcmp 'mamma'</syntaxhighlight>


Explanation:
Explanation:
Line 347: Line 347:
Task example:
Task example:


<lang J> ('allow' wrdcmp 'lolly')&{&.;: 'gray yellow green'
<syntaxhighlight lang="j"> ('allow' wrdcmp 'lolly')&{&.;: 'gray yellow green'
yellow yellow green gray gray</lang>
yellow yellow green gray gray</syntaxhighlight>


=={{header|JavaScript}}==
=={{header|JavaScript}}==
<lang javascript>(() => {
<syntaxhighlight lang="javascript">(() => {
"use strict";
"use strict";


Line 542: Line 542:
// MAIN ---
// MAIN ---
return main();
return main();
})();</lang>
})();</syntaxhighlight>
{{Out}}
{{Out}}
<pre>ALLOW -> LOLLY -> [1,1,2,0,0] -> amber amber green gray gray
<pre>ALLOW -> LOLLY -> [1,1,2,0,0] -> amber amber green gray gray
Line 559: Line 559:
{{works with|jq}}
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
'''Works with gojq, the Go implementation of jq'''
<lang jq>def colors: ["grey", "yellow", "green"];
<syntaxhighlight lang="jq">def colors: ["grey", "yellow", "green"];
def wordle($answer; $guess):
def wordle($answer; $guess):
Line 595: Line 595:
| wordle(.[0]; .[1]) as $res
| wordle(.[0]; .[1]) as $res
| ($res | map(colors[.])) as $res2
| ($res | map(colors[.])) as $res2
| "\(.[0]) v \(.[1]) => \($res) => \($res2)"</lang>
| "\(.[0]) v \(.[1]) => \($res) => \($res2)"</syntaxhighlight>
{{out}}
{{out}}
As for [[#Wren]].
As for [[#Wren]].
Line 601: Line 601:
=={{header|Julia}}==
=={{header|Julia}}==
{{trans|Wren}}
{{trans|Wren}}
<lang julia>const colors = ["grey", "yellow", "green"]
<syntaxhighlight lang="julia">const colors = ["grey", "yellow", "green"]
function wordle(answer, guess)
function wordle(answer, guess)
Line 635: Line 635:
res2 = [colors[i + 1] for i in res]
res2 = [colors[i + 1] for i in res]
println("$pair0 v $pair1 => $res => $res2")
println("$pair0 v $pair1 => $res => $res2")
end</lang>{{out}}
end</syntaxhighlight>{{out}}
<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"]
Line 645: Line 645:


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>#!/usr/bin/perl
<syntaxhighlight lang="perl">#!/usr/bin/perl


use strict; # https://rosettacode.org/wiki/Wordle_comparison
use strict; # https://rosettacode.org/wiki/Wordle_comparison
Line 658: Line 658:
print "@$test => @{[ qw( green yellow grey )
print "@$test => @{[ qw( green yellow grey )
[map ord, split //, s/.*\n//r =~ tr/\0\1/\2/cr] ]}\n";
[map ord, split //, s/.*\n//r =~ tr/\0\1/\2/cr] ]}\n";
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 671: Line 671:
<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.<br>
<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.<br>
In practice the for loop takes a private copy of the value of the limit, be that n or more significantly say length(s), and ignores any changes you might subsequently make to it.</small>
In practice the for loop takes a private copy of the value of the limit, be that n or more significantly say length(s), and ignores any changes you might subsequently make to it.</small>
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight 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>
<span style="color: #008080;">function</span> <span style="color: #000000;">wordle</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">answer</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">guess</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">wordle</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">answer</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">guess</span><span style="color: #0000FF;">)</span>
Line 705: Line 705:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s v %s =&gt; %v =&gt; %v\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">answer</span><span style="color: #0000FF;">,</span><span style="color: #000000;">guess</span><span style="color: #0000FF;">,</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">rac</span><span style="color: #0000FF;">})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s v %s =&gt; %v =&gt; %v\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">answer</span><span style="color: #0000FF;">,</span><span style="color: #000000;">guess</span><span style="color: #0000FF;">,</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">rac</span><span style="color: #0000FF;">})</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>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 723: Line 723:


{{trans|Go}}
{{trans|Go}}
<lang Picat>main =>
<syntaxhighlight lang="picat">main =>
Pairs = [["ALLOW", "LOLLY"],
Pairs = [["ALLOW", "LOLLY"],
["BULLY", "LOLLY"],
["BULLY", "LOLLY"],
Line 756: Line 756:
Presentation[I] := [to_lowercase(Guess[I])] ++ "*"
Presentation[I] := [to_lowercase(Guess[I])] ++ "*"
end
end
end.</lang>
end.</syntaxhighlight>


{{out}}
{{out}}
Line 768: Line 768:


=={{header|Python}}==
=={{header|Python}}==
<lang python>'''Wordle comparison'''
<syntaxhighlight lang="python">'''Wordle comparison'''


from functools import reduce
from functools import reduce
Line 941: Line 941:
# MAIN ---
# MAIN ---
if __name__ == '__main__':
if __name__ == '__main__':
main()</lang>
main()</syntaxhighlight>
{{Out}}
{{Out}}
<pre>Target -> Guess -> Scores
<pre>Target -> Guess -> Scores
Line 958: Line 958:
=={{header|Quackery}}==
=={{header|Quackery}}==


<lang Quackery> [ tuck witheach
<syntaxhighlight lang="quackery"> [ tuck witheach
[ over i^ peek = while
[ over i^ peek = while
2 rot i^ poke
2 rot i^ poke
Line 975: Line 975:
$ "ROBIN" $ "SONIC" wordle-compare echo cr
$ "ROBIN" $ "SONIC" wordle-compare echo cr
$ "ROBIN" $ "ROBIN" wordle-compare echo cr
$ "ROBIN" $ "ROBIN" wordle-compare echo cr
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 990: Line 990:
=={{header|Raku}}==
=={{header|Raku}}==
Updated to add a proof of concept matching for similarity where commonly found on spoofing domain names. Of course this is just the tip of the iceberg (only comparing results after decomposition) and there are way too many [https://util.unicode.org/UnicodeJsps/confusables.jsp Unicode homoglyphs] that can only be matched using a [http://www.unicode.org/Public/security/8.0.0/confusables.txt lookup table/database].
Updated to add a proof of concept matching for similarity where commonly found on spoofing domain names. Of course this is just the tip of the iceberg (only comparing results after decomposition) and there are way too many [https://util.unicode.org/UnicodeJsps/confusables.jsp Unicode homoglyphs] that can only be matched using a [http://www.unicode.org/Public/security/8.0.0/confusables.txt lookup table/database].
<lang perl6># 20220216 Raku programming solution
<syntaxhighlight lang="raku" line># 20220216 Raku programming solution


sub wordle (\answer,\guess where [==] (answer,guess)».chars ) {
sub wordle (\answer,\guess where [==] (answer,guess)».chars ) {
Line 1,020: Line 1,020:
say .[0]~' vs '~.[1]~"\t"~ wordle .[0],.[1] for (
say .[0]~' vs '~.[1]~"\t"~ wordle .[0],.[1] for (
<ALLOW LOLLY>, <ROBIN ALERT>, <ROBIN SONIC>, <ROBIN ROBIN>, <BULLY LOLLY>,
<ALLOW LOLLY>, <ROBIN ALERT>, <ROBIN SONIC>, <ROBIN ROBIN>, <BULLY LOLLY>,
<ADAPT SÅLÅD>, <Ukraine Ukraíne>, <BBAABBB BBBBBAA>, <BBAABBB AABBBAA> );</lang>
<ADAPT SÅLÅD>, <Ukraine Ukraíne>, <BBAABBB BBBBBAA>, <BBAABBB AABBBAA> );</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,035: Line 1,035:


=={{header|Swift}}==
=={{header|Swift}}==
<lang swift>enum Colour : CustomStringConvertible {
<syntaxhighlight lang="swift">enum Colour : CustomStringConvertible {
case grey
case grey
case yellow
case yellow
Line 1,080: Line 1,080:
print("\(pair.0) v \(pair.1) => \(result)")
print("\(pair.0) v \(pair.1) => \(result)")
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,092: Line 1,092:


=={{header|Tailspin}}==
=={{header|Tailspin}}==
<lang tailspin>
<syntaxhighlight lang="tailspin">
templates wordle
templates wordle
sink removeFirst
sink removeFirst
Line 1,119: Line 1,119:


['ALLOW', 'LOLLY'] -> wordle -> !OUT::write
['ALLOW', 'LOLLY'] -> wordle -> !OUT::write
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 1,128: Line 1,128:
=={{header|Vlang}}==
=={{header|Vlang}}==
{{trans|Go}}
{{trans|Go}}
<lang vlang>fn wordle(answer string, guess string) []int {
<syntaxhighlight lang="vlang">fn wordle(answer string, guess string) []int {
n := guess.len
n := guess.len
if n != answer.len {
if n != answer.len {
Line 1,168: Line 1,168:
println("${pair[0]} v ${pair[1]} => $res => $res2\n")
println("${pair[0]} v ${pair[1]} => $res => $res2\n")
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,181: Line 1,181:


=={{header|Wren}}==
=={{header|Wren}}==
<lang ecmascript>var colors = ["grey", "yellow", "green"]
<syntaxhighlight lang="ecmascript">var colors = ["grey", "yellow", "green"]


var wordle = Fn.new { |answer, guess|
var wordle = Fn.new { |answer, guess|
Line 1,215: Line 1,215:
var res2 = res.map { |i| colors[i] }.toList
var res2 = res.map { |i| colors[i] }.toList
System.print("%(pair[0]) v %(pair[1]) => %(res) => %(res2)")
System.print("%(pair[0]) v %(pair[1]) => %(res) => %(res2)")
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,227: Line 1,227:


=={{header|XPL0}}==
=={{header|XPL0}}==
<lang XPL0>string 0;
<syntaxhighlight lang="xpl0">string 0;


proc ShowColors(Result);
proc ShowColors(Result);
Line 1,258: Line 1,258:
ShowColors(Wordle("ROBIN", "SONIC"));
ShowColors(Wordle("ROBIN", "SONIC"));
ShowColors(Wordle("ROBIN", "ROBIN"));
ShowColors(Wordle("ROBIN", "ROBIN"));
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}
Line 1,271: Line 1,271:
=={{header|Yabasic}}==
=={{header|Yabasic}}==
{{trans|Phix}}
{{trans|Phix}}
<lang Yabasic>// Rosetta Code problem: http://rosettacode.org/wiki/Wordle_comparison
<syntaxhighlight lang="yabasic">// Rosetta Code problem: http://rosettacode.org/wiki/Wordle_comparison
// by Galileo, 02/2022
// by Galileo, 02/2022


Line 1,312: Line 1,312:
print
print
next
next
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>ALLOW v LOLLY => yellow yellow green grey grey
<pre>ALLOW v LOLLY => yellow yellow green grey grey