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';If you'd like, you can get more lines by adjusting the line that begins with 'exit()'.
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;
}