I'm a little rusty, but this looked fun and I took a stab at it. Here's a convaluted, winding, unoptimized memory hog that generates a high scoring solution, but not always the highest possible score given arbitrary lines of input...
#!/usr/bin/perl -w
use strict;
my @lines=<DATA>;
my %hash = ();
my %highscores = ();
my @final_words = ();
my $final_score = 0;
my $longword = '';
for my $letter ( "a" .. "z" ) {
my $line = '';
for (@lines) {
$line = $_;
my @words = split;
for my $word (@words) {
my $tmp = scalar grep {/$letter/} split (//,$word);
if (!$hash{$line}{$letter}{highscore} || $hash{$line}{$letter}{high
+score} < $tmp) {
$hash{$letter}{$line}{highscore} = $tmp;
$hash{$letter}{$line}{word} = $word;
$hash{$line}{$letter}{highscore} = $tmp;
$hash{$line}{$letter}{word} = $word;
}
}
}
$highscores{$letter} = 0;
$highscores{$letter} += $hash{$letter}{$_}{highscore} for keys %{$has
+h{$letter}};
if (!$highscores{bigletter}{score} || $highscores{bigletter}{score} <
+ $highscores{$letter}) {
$highscores{bigletter}{score} = $highscores{$letter};
$highscores{bigletter}{letter} = $letter;
}
}
my $bl = $highscores{bigletter}{letter};
print "Letter with highest score is \"$bl\" with $highscores{bigletter
+}{score} occurrences\n";
for my $line (keys %{$hash{$bl}}) {
push @final_words,$hash{$line}{$bl}{word};
}
print "\nWords chosen:\n";
print "$_\n" for @final_words;
$longword .= $_ for @final_words;
my @final_letters = split (//,$longword);
for my $letter ( "a" .. "z" ) {
my $count = grep {/$letter/} @final_letters;
$final_score += ($count * $count + $count) / 2;
}
print "\nFinal score: $final_score\n";
__DATA__
alabama arkansas alaska delaware hawaii indiana kansas montana delawar
+e
sazerac sangrita
radiator gastank engine heater fender wheel detent battery clutch mirr
+or window
alloy amalgam vanadium copper steel
rutabaga limabean cress carrot sorrel squash cabbage pepper lettuce be
+et leek celery endive rhubarb parsnip pumpkin
azalia camellia dahlia gardenia gentian vervain canna hepatica bluebel
+l anemone oleander
lasagna macaroni pastina gnocchi tortelli alfabeto
mascagni britten menotti
unamas tacotime pizzahut tacobell panago tacomayo edojapan hardees
caramel marzipan taiglach taffy brittle fondant toffee dragee
__OUTPUT__
Letter with highest score is "a" with 26 occurrences
Words chosen:
alabama
unamas
sazerac
radiator
lasagna
amalgam
caramel
azalia
mascagni
rutabaga
Final score: 457
Note the score is less than the shown maximum of 474 for this data set.