Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: A Word Game (my solution)

by jdporter (Paladin)
on Feb 15, 2022 at 14:41 UTC ( [id://11141398]=note: print w/replies, xml ) Need Help??


in reply to A Word Game (Update 3)

# 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__

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11141398]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-25 15:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found