in reply to Cheat at Scrabble
Fun little problem. thanks.
In the spirit of TIMTOWTDI, here's a solution that does not use permutation
or perl's sort.
#!/usr/bin/perl # https://perlmonks.org/?node_id=1224403 use strict; use warnings; use Path::Tiny; use List::Util 'sum'; use Getopt::Long; GetOptions( 'dl=i' => \my @dl, 'tl=i' => \my @tl, 'dw' => \my $dw, 'tw' => \my $tw, ); my %worth = ( a => 1, b => 3, c => 3, d => 2, e => 1, f => 4, g => 2, h => 4, i => 1, j => 8, k => 5, l => 1, m => 3, n => 1, o => 1, p => 3, q => 10, r => 1, s => 1, t => 1, u => 1, v => 2, w => 2, x => 8, y => 4, z => 10, ); my $input = shift // die "No Input!\n"; my $length = length $input; my $pattern = qr/@{[ join '', map "$_?", sort split '', $input ]}/; my @best; push @{ $best[score($_)] }, $_ for my @legalwords = grep { length() <= $length and join('', sort split //) =~ /^$pattern +$/ } path( '/usr/share/dict/words' )->lines({chomp => 1}); print <<END; input = $input legalwords = @{[scalar @legalwords]} best score = $#best best words = @{ $best[-1] } END sub score { my @eachvalue = @worth{ my @chars = split //, shift }; @eachvalue > $_ and $eachvalue[$_ - 1] *= 2 for @dl; @eachvalue > $_ and $eachvalue[$_ - 1] *= 3 for @tl; my $value = sum @eachvalue, @chars == 7 && 50; $value *= ($dw ? 2 : 1) * ($tw ? 3 : 1); }
outputs
input = eoaprzn legalwords = 67 best score = 14 best words = zap
|
|---|