Here's my solution. It's 184 characters:
sub mu { for(@s=($_[0],$c=0);$c<$_[2];){($_=shift@s or$c++,$s[@s]=0,next )eq$_[1]&&return$c;$s[@s]="$_$1"if/^M/;/I$/and$s[@s]=$_.U;$s[ @s]="$`$'"while/UU/g;$s[@s]=$`.U.substr$',2while/I(?=II)/g}0 }
I believe it handles all the cases correctly. Here are the tests I used:
print mu( "MUUUU", "M", 20 ); # returns 2 print mu( "MI", "MU", 7 ); # returns 0 print mu( "MI", "MI", 20 ); # returns 0 print mu( "MI", "MUIUI", 20); # returns 4 print mu( "MI", "MUIUI", 3 ); # returns 0 print mu( "MIIII", "MIU", 4); # returns 1 print mu( "MI", "MIIU", 4); # returns 2
The second to last line tests rule 3 where there are four Is in a row, and the last line tests rule 1.

I switched the limit on the second test from 20 to 7 because I didn't have the patience to wait for the sub to run through all the possibilities to 20 steps. :)


In reply to Re: (Golf) Gödel, Escher, and Bach, Oh My! by chipmunk
in thread (Golf) Gödel, Escher, and Bach, Oh My! 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.