Limbic~Region has asked for the wisdom of the Perl Monks concerning the following question:
I set out to write code that could solve the following style of word puzzle:
- The mystery word is 5 letters long
- The mystery word has 2 letters in common with 'blahasdf'
- The mystery word has 3 letters in common with 'golfborf'
- The mystery word has 0 letters in common with 'abcdefg'
- It is possible for a hint word to have 0 letters in common
- It is possible for the mystery and hint words to contain duplicate letters
- The number of letters in common is the exact number of unique letters in common. A 't' would only be counted once even if it existed twice in both the mystery word and the hint word
- There may only be one set of letters for the mystery word, but those letters may be able to form one or more mystery words, so all solutions are required
- The mystery word(s) should be able to be found in most any english dictionary
- Punctuation and capitalization should be completely ignored
- There will be no letters in the mystery word that are not covered by the hint words. Added 2005-01-12 16:10:00 EST
I had an idea in my mind of how to code it, but haven't gotten very far. You can see the unfinished code below. I don't want anyone to think that I haven't at least put forth a little effort before asking.
#!/usr/bin/perl use strict; use warnings; use Mystery::Word; my $puzzle = Mystery::Word->new( size => 5 ); $puzzle->hint( bumps => 2, seams => 2, domes => 3, shake => 3, pokes => 3, dukes => 3 ); my @solutions = $puzzle->solve(); print "$_\n" for @solutions; __END__ ################################## package Mystery::Word; use strict; use warnings; use Carp; use constant WORD => 0; use constant SIZE => 1; use constant COMMON => 2; use constant COMBOS => 3; our $VERSION = '0.01'; sub new { my $class = shift; croak "Incorrect number of parameters for 'new'" if @_ % 2; my $self = bless {}, $class; $self->_init( @_ ); return $self; } sub _init { my ($self, %opt) = @_; $opt{size} = force_numeric( $opt{size} ); $opt{size} ||= 0; croak "'size' parameter must be set to a positive whole integer" i +f ! $opt{size}; $self->{target} = $opt{size}; } sub hint { my $self = shift; croak "Incorrect number of parameters for 'hint'" if @_ % 2; my %hint = @_; for my $word ( keys %hint ) { carp "$word contains non alpha characters" if $word =~ tr/a-z/ +/c; push @{ $self->{hint} }, [ lc $word, length $word, force_numeric( $hint{ $word } ), combinations(length $word, $hint{ $word }) ]; } } sub combinations { my ($n, $k) = @_; return factorial( $n ) / ( factorial( $k ) * factorial( $n - $k ) +); } sub factorial { my $n = shift; my $factorial = 1; $factorial *= $_ for 1 .. $n; return $factorial; } sub force_numeric { my $num = shift; return 0 if ! defined $num || ! length $num; no warnings 'numeric'; return abs( int( $num ) ); } sub solve { my $self = shift; @{ $self->{hint} } = map { $_->[0] } sort { $a->[1] <=> $b->[1] || $b->[2] <=> $a->[2] } map { [ $_, $_->[COMBOS], $_->[COMMON] ] } @{ $self->{hint} } +; # Here is where I got stuck } "This statement is false";
- Write code that can solve any given puzzle
- Write code that can create a puzzle given a mystery word
Cheers - L~R
Update: Prior to any replies
Added two more of my assumptions after a CB discussion (dragonchild & bart). I also emphasized that the code was just my failed attempt but is being provided to show that I did put forth some effort (bart). Also corrected a copy/paste error (wolfger).
Update2: 2005-01-12 15:30:00 EST
After noticing potential copyright issues with the two example puzzles, the hint words have been replaced and no longer form a valid puzzle - sorry. If trammell's solution works, you can always generate test puzzle with it.