#!/usr/bin/perl use 5.010; use strict; use warnings; use Getopt::Long; use List::Util 'sum'; my %tiles = qw [ 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 4 W 4 X 8 Y 4 Z 10 ]; die "No rack?\n" unless @ARGV; my ($rack, $blanks) = (@ARGV, 0); my $full = length($rack) + $blanks == 7; my %target; $target{$_}++ for split //, $rack; my @words = `cat /usr/share/dict/words`; chomp @words; my @good; my %discount; WORD: foreach my $word (@words) { next unless $word =~ /^[a-z]+$/; my %copy = %target; my $b = 0; my $d = 0; foreach my $c (split //, $word) { if (--$copy{$c} < 0) { $b++; $d += $tiles{uc $c}; $discount{$word} = $d; } next WORD if $b > $blanks; } push @good, $word; } my $tl = length($rack) + $blanks; @good = sort {$a->[1] <=> $b->[1]} map {[$_, sum (map {$tiles{uc $_}} split //) - ($discount{$_} || 0) + ($full && length($_) == $tl ? 50 : 0)]} @good; printf "%2d: %s\n", $_->[1], $_->[0] for @good; __END__