[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] Jotto puzzle: first five guesses [spoilers]




>
> > How can you efficiently find all sets of five words from
> > that list which use up 25 unique letters of the alphabet.
> >
> > What's the fastest code with unlimited RAM?
> >
> > What's the fastest code with minimal RAM use (probably lots
> > of disk use)?
> >
> > Any takers?

Am I missing something here, or is this simply saying, "find all groups of 
five 5-letter words with no repeated letters?"  I took a straightforward 
recursive approach (okay, it took a few hours :-) and found none using the 
ENABLE list.  However, it does provide a nice running display of where it's 
gotten to, I found it quite hard to tear myself away :-)

use strict;
$|++;
my (@words, %anagram);
use constant LEN => 5;

open (W, 'enable.words') or die "$!\n";
while (<W>) {
   chomp;
   next unless length == LEN;
   my %let;
   @let{split //} = ();
   next if keys %let != LEN or $anagram{join '', sort keys %let}++;
   push @words, $_;
}

print @words . " eligible words found: ",
       join (' ', @words[0..4]), "...\n";

try ([], @words);


sub try {
   my @cur  = @{shift()};
   do { print "\r@cur\n" } && return if @cur == int (26 / LEN);
   my $letters = $cur[-1] if @cur;
   my @rest = $letters ? grep !/[$letters]/, @_ : @_;
   while (@rest) {
     my $word = shift @rest;
     print ' 'x30, "\r", join ' ', @cur, $word;
     try ([@cur, $word], @rest);
   }
}

3877 eligible words found: abets abhor abide abler ables...

Note: assumes dictionary is one word per line, lowercase letters only.



--
Peter Scott
Pacific Systems Design Technologies


==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe