Skip to content

Instantly share code, notes, and snippets.

@WebDragon
Last active July 11, 2024 16:37
Show Gist options
  • Save WebDragon/fe7b6a4823191fb8f4080c0788a22dfb to your computer and use it in GitHub Desktop.
Save WebDragon/fe7b6a4823191fb8f4080c0788a22dfb to your computer and use it in GitHub Desktop.
a stab at a commandline wordle helper in the absence of an actual list of wordle words
#!/usr/bin/perl
use warnings;
use strict;
use v5.18;
use Getopt::Long;
my ($exact, $letterlist, $not, @letters, @miss);
GetOptions (
"exact|e=s" => \$exact,
"letters|l=s{1,5}" => \@letters,
"miss|m=s{1,}" => \@miss,
"not|n=s" => \$not,
) or die ("Error in command line arguments\n");
@letters = split(//, join(',', @letters));
@miss = split(/,/, join(',', @miss));
my $exactmatch = $exact ? qr/$exact/ : undef;
foreach my $letter (@letters) {
$letterlist .= "(?=.*$letter)";
}
my $letters = $letterlist ? qr/\A$letterlist/s : undef;
my $misses = join('|', @miss) if @miss;
my $miss = $misses ? qr/$misses/ : undef;
my $nots = $not ? qr/[$not]/ : undef;
open (my $words, '<', '/usr/share/dict/words') or die "Cannot open words file: $!";
my @wordlist;
while ( <$words> ) {
next unless m/^[a-z]{5}$/;
next unless ( $exactmatch ? $_ =~ $exactmatch : 1 );
next unless ( $letters ? $_ =~ $letters : 1 );
next unless ( $miss ? $_ !~ $miss : 1);
next unless ( $nots ? $_ !~ $nots : 1);
push @wordlist, $_;
}
use List::Util qw[sample shuffle];
print for sample 20, shuffle @wordlist;
@WebDragon
Copy link
Author

WebDragon commented Jul 10, 2024

example usage

~>$ wordlehelper --not cranopkymu --letters edg --miss ...e.,..e..,...g.,....e --exact ....d
gelid
getid
getfd
~>$ wordlehelper --exact c.... --letters ahe --miss ..a..,....e,.a... --not lrnd
cheat
cheka
cheap

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment