# given a wordle W and a guess G, calculate the result.
package Wordle;
use strict;
use warnings;
# returns a vector of match codes: 2=right/right; 1=right/wrong; 0=w
+rong.
sub guess
{
my( $wordl, $guess ) = @_;
my @result=(0)x5;
# first, look for the right/right:
for my $i ( 0 .. 4 )
{
if ( substr($wordl, $i, 1) eq substr($guess, $i, 1) )
{
$result[$i] = 2;
substr($wordl, $i, 1) = ' ';
substr($guess, $i, 1) = ' ';
}
}
$guess eq ' ' and return(5,0); # done
# second, look for the right/wrong:
for my $i ( 0 .. 4 )
{
my $l = substr $guess, $i, 1;
next if $l eq ' ';
if ( $wordl =~ /$l/ )
{
$result[$i] = 1;
substr($wordl, $i, 1) = ' ';
substr($guess, $i, 1) = ' ';
}
}
@result
}
# returns a vector of counts ( right/right, right/wrong )
sub numeric_guess
{
my( $wordl, $guess ) = @_;
# first, look for the right/right:
my $right=0;
for my $i ( reverse( 0 .. 4 ) )
{
if ( ord(substr $wordl, $i, 1) eq ord(substr $guess, $i, 1) )
{
$right++;
substr($wordl, $i, 1) = '';
substr($guess, $i, 1) = '';
}
}
$right == 5 and return(5,0); # done
# second, look for the right/wrong:
my $wrong = 0;
for my $l ( split '', $guess )
{
$wordl =~ s/$l// and
$wrong++;
}
join '', ($right, $wrong)
}
__PACKAGE__