« Headphone Amps. Audio Hardware You Didn't Know You Needed | Main

Morris Puzzle Progression Done With Perl

A while back my boss asked us to complete the Morris puzzle for fun. What's the Morris puzzle you ask?

What's the next sequence in this set?

1
11
21
1211
111221

After figuring it out, I couldn't help but want to write a solution in perl. Below is a Perl script that will carry out the progression to 16 lines. Note, this was written to be terse, not readable.

$s = '1';

BLOCK: {
     print "$s\n";

     exit() if ( length( $s ) > 100 );
     
     @nums = split( //, $s );
     @uniq = ();
     $tmp = ();
     
     foreach my $num ( @nums ){
          push( @uniq, $num ) if ( $num != $uniq[-1] );
     }
     
     foreach my $num ( @uniq ) {
          $s =~ s/^($num+)//;
          $tmp .= ( length( $1 ) . $num );
     }
     
     $s = $tmp;
     goto BLOCK;
}
If you'd like, you can get more lines by adjusting the line that begins with 'exit()'.

TrackBack

TrackBack URL for this entry:
http://www.aviditysoftware.com/cgi-bin/mt/mt-tb.cgi/21

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

About

This page contains a single entry from the blog posted on September 10, 2007 12:31 PM.

The previous post in this blog was Headphone Amps. Audio Hardware You Didn't Know You Needed.

Many more can be found on the main index page or by looking through the archives.