So I was golfing down my code for my Game of Life and had gotten down to 287 characters. But I could use one of the tricks I thought up to shave down teamster_jr's version to 285 characters, so I wasn't going to claim the lead yet.

I flipped around the logic in the main conditional and shaved it down to 283 characters (I thought) and figured that would do it. But instead of playing the game of life, the gameboard instantly converged on the sierpinski gasket. That's right - instead of creating a shorter version of the game of life, I'd accidentally created a fractal generator.

I went through and ripped out all the unnecessary game of life stuff from it (looping, timers, random seeding of cells, etc.) and the result is a pure ascii output of the sierpinski gasket. I also re-formatted it to be somewhat easier to read, but remember, this is the result of golfing and obfuscation, so it still won't be that easy to read.

my $s = 150; @a = 0..$s; for $r (@a) { $b[$r][$_] = $" for @a } { for $r (@a) { for (@a) { $h = 0; for $i ($r-1..$r+1) { for $j ($_-1..$_+1) { $h++ if $b[$i][$j] ne $" & $i >= 0 & $i <= $s & $j >= 0 & $j <= $s & $i ^ $r & $j ^ $_ } } $e[$r][$_] = $h == 3 ? 0 : $b[$r][$_] ne $" & $h == 2 ? 0 : $" } } print map{ @{$e[$_]}, $/ } @a; }

I just think it's incredible that an incorrect application of DeMorgan's rules was all it took to turn the game of life into the sierpinski gasket. Stephen Wolfram would be proud of me. It sure made my day.

Replies are listed 'Best First'.
Re: The accidental fractal
by jwkrahn (Monsignor) on Sep 23, 2006 at 21:14 UTC
    You can shorten this:
    for $r (@a) { $b[$r][$_] = $" for @a }
    To this:
    @b = ([($")x@a])x@a;
    You can also shorten this:
    print map{ @{$e[$_]}, $/ } @a;
    using the for statment modifier:
    print @{$e[$_]}, $/ for @a;