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

In reply to Re: Cheat at Scrabble by tybalt89
in thread Cheat at Scrabble by 1nickt

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.