Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Challenge: Mystery Word Puzzle

by Roy Johnson (Monsignor)
on Jan 12, 2005 at 21:15 UTC ( [id://421778]=note: print w/replies, xml ) Need Help??


in reply to Challenge: Mystery Word Puzzle

Not a module, and not OO, but a working solution:
#!perl -l use strict; use warnings; sub sort_word { return join '', sort split //, $_[0]; } my %hints = ( bumps => 2, seams => 2, domes => 3, shake => 3, pokes => 3, dukes => 3 ); my $wordlen = 5; my %dict; open(DICT, '/usr/dict/words') or die "$!: /usr/dict/words\n"; chomp, push @{$dict{sort_word($_)}}, $_ while (<DICT>); close(DICT); my $guess = 'a' x $wordlen; sub letters_in_common { my ($str1, $str2) = @_; my %u = map {$_ => $_} split //, $str1; return scalar(grep defined, delete @u{split //, $str2}); } while (length($guess) == $wordlen) { # Check criteria my $match = 1; while (my ($k,$v) = each %hints) { if (letters_in_common($guess, $k) != $v) { $match = 0; keys %hints; # Reset each last; } } # If match, check dictionary and print results. Done! # Otherwise, increment guess if ($match) { if (exists($dict{$guess})) { print "Solution(s): @{$dict{$guess +}}"; last } else { print "No words match qualifying guess $guess"; } } ++$guess; # Short-circuit: only keep guesses that are sorted # So when the last char flips to an a, flip the trailing # string of last chars to a repetition of the rightmost # non-a $guess =~ s/([^a])(a+)$/$1 x (length($2)+1)/e; }
Update! It works, but I feel pretty stupid for all the work of going through all possible guesses when I should just go through the dictionary keys! Gee, it sure is faster that way!

Caution: Contents may have been coded under pressure.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://421778]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-04-20 03:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found