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.

In reply to Re: (Golf) The Perl Boggles by chipmunk
in thread (Golf) The Perl Boggles by Masem

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.