Weighing in at 425 characters. This is my first ever attempt at Perl golf, so I'm not sure how good or bad it is. I won't tell you how long I spent on it. :-)
sub test_boggle_word { ($w,$n,@b)=@_;for(@b){push@{$h{$_}},$x++}@c=split'',$w;for(@c){return +0if!$h{$_}}@l=map{$h{$_}}@c;$c=-1;$i=-1;my@t;I:while(1){$j=$i+1;$t[$j +]=@t>$j?$t[$j]+1:0;while($t[$j]<@{$l[$j]}){$t=$l[$j][$t[$j]];$a=$t;$b +=$c;($a,$b)=($b,$a)if($a>$b);if($a==-1||($b-$a==1&&$b%$n>0)||($b-$a== +$n-1&&$a%$n>0)||$b-$a==$n||($b-$a==$n+1&&$b%$n>0)){return 1if$i==@l-2 +;++$i;$c=$t;next I}++$t[$j]}--$i;pop@t;lastif$i<-1;$c=$i==-1?-1:$l[$i +][$t[$i]]} }
I didn't bother using the prepare_boggle_search() function. Also, I wasn't sure if creating extra functions was allowed, so I used iteration rather than recursion (which would have been more natural). This solution also is able to use the same letter more than once, but not consecutively (which was expressly forbidden). I hope that's okay. And for bonus points, it should work for any Boggle board of size NxN.

It's extremely hard to read all smushed up like that, so here's a little bit nicer version:

sub test_boggle_word { ($w,$n,@b)=@_; for(@b){push@{$h{$_}},$x++} @c=split'',$w; for(@c){return 0 if!$h{$_}} @l=map{$h{$_}}@c; $c=-1; $i=-1; my@t; I:while(1){ $j=$i+1; $t[$j]=@t>$j?$t[$j]+1:0; while($t[$j]<@{$l[$j]}){ $t=$l[$j][$t[$j]]; $a=$t; $b=$c; ($a,$b)=($b,$a)if($a>$b); if($a==-1||($b-$a==1&&$b%$n>0)||($b-$a==$n-1&&$a%$n>0)|| $b-$a==$n||($b-$a==$n+1&&$b%$n>0)){ return 1 if$i==@l-2; ++$i; $c=$t; next I } ++$t[$j] } --$i; pop@t; last if$i<-1; $c=$i==-1?-1:$l[$i][$t[$i]] } }
Any thoughts?

In reply to Re: (Golf) The Perl Boggles by thelenm
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.