Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re: (Golf) The Perl Boggles

by chipmunk (Parson)
on Mar 02, 2002 at 20:05 UTC ( #148877=note: print w/replies, xml ) Need Help??

in reply to (Golf) The Perl Boggles

This golf challenge is tricky, because the design is opposite from how one would usually approach Boggle. Not that this is a bad thing; it makes the challenge more interesting!

Usually, when playing Boggle, you start with a list of words, and search the grid for all the words in the list. (In real life, the word list is your vocabulary. In a program, it might be /usr/dict or some other word list.) In this golf challenge, you start with the grid, and then you get one word at a time that you try to find, and you can stop searching as soon as you find the word.

Here's my solution. I went for the bonus points using $n as the board size. It's 195 characters; surely a shorter solution is possible!

sub test_boggle_word { # 1 2 3 4 5 #2345678901234567890123456789012345678901234567890 my$t;for$y(@n=0..$n-1){$t|=t($y,$_,@_)for@n}sub t{ my($y,$x,$w,$n,@g,$t)=@_;if($y>=0&$y<$n&$x>=0&$x<$ n&chop$w eq splice@g,$y*$n+$x,1,0){for$Y(-1..1){$t |=t($y+$Y,$x+$_,$w,$n,@g)|!$w for-1..1}}$t}$t } # testing code: @bogglelist = qw/ R E W E M T A S K L E T N V F O /; $n = 4; $dict = [ qw/ WATER SETTER VETO WASTE WASTES / ]; print "$_\n" for find_boggle_words(); sub find_boggle_words { # $storage = prepare_boggle_search( $n, @bogglelist ); @matched = grep { test_boggle_word( $_, $n, @bogglelist ) } @$dict; return @matched; }
In my testing code, WATER, VETO and WASTE can be found in the grid. The original node doesn't specify, but in the rules of Boggle a single cube cannot be used twice in the same word. So, WASTES can't be found because the S would be used twice.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://148877]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2017-09-24 23:22 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (275 votes). Check out past polls.