in reply to Challenge: Letter Power
My first attempt was brute force, unremarkable, found the 474 after processing 95% of the combinations and took just under 8 hours to run.
My second finds the 474 in 0.15 seconds.
P:\test>499358-2 460 : [ alabama sangrita radiator amalgam rutabaga camellia lasagna ma +scagni tacomayo marzipan ] 472 : [ alabama sangrita radiator amalgam rutabaga azalia lasagna masc +agni tacomayo marzipan ] 474 : [ alabama sangrita radiator amalgam rutabaga azalia lasagna masc +agni tacomayo taiglach ] Elapsed: 0.015 seconds.
See comments for the strategy used.
#! perl -slw use strict; use Time::HiRes qw[ time ]; my $start = time; my @lookup = map{ ($_+1) * $_ /2 } 0 .. 80; sub calcScore { my( %chars, $score ); $chars{ $_ }++ for map{ split'' } @{ $_[0] }; $score += $lookup[ $_ ] for values %chars; return $score; } my @cats = map{ [ split ' ' ] } <DATA>; close DATA; ## Sort the words in each category by their individual scores (descend +ing) my %wordScores; @{ $cats[ $_ ] } = sort{ ( $wordScores{ $b } ||= calcScore [$b] ) <=> ( $wordScores{ $a } ||= calcScore [$a] ) } @{ $cats[ $_ ] } for 0 .. $#cats; ## form our guess from the highest scoring words in each category my @guess = map{ $cats[ $_ ][ 0 ] } 0 .. $#cats; my $best = calcScore \@guess; ## Loop over the categories swaping the other words in that cateory ## for the guessed word. If we get a better score, start again. LOOP: for my $iCat ( 0 .. $#cats ) { my $cat = $cats[ $iCat ]; for my $iWord ( 1 .. $#$cat ) { my $test = calcScore [ @guess[ 0 .. $iCat -1 ], $cat->[ $iWord ], @guess[ $iCat+1 .. $#guess ] ]; if( $test > $best ) { $best = $test; $guess[ $iCat ] = $cat->[ $iWord ]; print "$best : [ @guess ]"; redo LOOP; } } } printf "Elapsed: %.3f seconds.\n", time - $start; __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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Challenge: Letter Power
by Limbic~Region (Chancellor) on Apr 17, 2009 at 00:15 UTC | |
by BrowserUk (Patriarch) on Apr 18, 2009 at 04:41 UTC | |
by Limbic~Region (Chancellor) on Apr 18, 2009 at 14:18 UTC | |
by BrowserUk (Patriarch) on Apr 18, 2009 at 16:44 UTC | |
by Limbic~Region (Chancellor) on Apr 18, 2009 at 18:15 UTC | |
|