<?xml version="1.0" encoding="windows-1252"?>
<node id="1008395" title="Game of life ran by unpack function" created="2012-12-11 15:41:04" updated="2012-12-11 15:41:04">
<type id="1588">
obfuscated</type>
<author id="295576">
ambrus</author>
<data>
<field name="doctext">
&lt;p&gt;
The language designer said to the programmer: “Thank you for freeing me from my prejudices.  I understand now that a language must not try to force the programmers to follow any single paradigm, but should instead offer all the features the programmer may want to use.  As a reward, you may wish for any three features and I will add them to the language.”
&lt;p&gt;
The programmer replied: “I'd like a powerful domain-specific language for blowing up binary strings to small parts.”
&lt;p&gt;
The language designer granted the programmer's wish, and the programmer promptly tried it, saying

&lt;code&gt;
#!perl
use warnings; use strict; use 5.010; 
use Time::HiRes "sleep";

our $DELAY = 0.05;

# determine size of terminal
my $w = pack "S4", 24, 79;
ioctl STDIN, $_, $w for 0x40087468, 0x5413;
our($R, $C) = unpack "S2", $w; $R--;

our $b;
if (rand 3 &lt; 1) {
	# glider gun pattern
	$b = pack "(A$C)[Lx$R]", ("")x3, split /^/, "
                                  1
                                1 1
                      11      11            11
                     1   1    11            11
          11        1     1   11
          11        1   1 11    1 1
                    1     1       1
                     1   1
                      11 ";
} else {
	# generate random bitmap as starting state
	$b = pack "(A)*", map { rand 3 &lt; 1 } 0 .. 2*$R*$C;
}

system qw"tput clear";

while () {

	# display game board
	system qw"tput home";
	say for unpack "xx$C(a$C)$R", $b;

	sleep $DELAY; 

	# game of life evolution step
	no warnings "numeric";
	$b = 
		pack "xx$C(A)*xx$C", 
		unpack "(x7a/(x13)X4Ax!18)[(A$R)$C]", 
		pack "((a*)17xx!18)*x3", 
		unpack "((AAAX3AAA\@$C AXAAAXAx$C (X3AAA)2\@)$C)$R", 
		$b;

}

__END__
&lt;/code&gt;

&lt;p&gt;
The programmer watched the pretty patterns appearing in his terminal.  
&lt;p&gt;
After a while, the language designer asked “And your other wishes?”
&lt;p&gt;
The programmer's reply was “Why would I need other wishes?”

&lt;readmore title="Remarks"&gt;
&lt;hr&gt;
&lt;p&gt;
Remarks.
&lt;ol&gt;
&lt;li&gt;
The story is stolen from [http://xkcd.com/152/|xkcd strip 152: Hamster Ball], reinterpreted slightly.
&lt;li&gt;
In case you can't guess from the comments, this obfuscation shows the evolution of Conway's Game of Life cellular automaton, starting from either a completely random pattern or a fixed glider gun pattern.  The program runs forever, stop it with control-C if you get bored, then restart to get another random pattern or the glider gun.  
&lt;li&gt;
It may be worth to resize your terminal to larger so that you get a larger game board.
&lt;li&gt;
The glider gun also serves as an example to show how to edit the program to start from a pattern of your choice.
&lt;li&gt;
It is possible that the dialog is internal, the language designer and the programmer being just different aspects of the same person.
&lt;li&gt;
The programmer has used the word “explode”, but the language designer took liberty to name the feature “unpack” instead to avert [http://mathoverflow.net/questions/53122/mathematical-urban-legends/53738#53738|curious incidents in case programmers obliviously discuss the feature in an airport waiting room].  He was thus wiser than the designers of Standard ML, as that language has a function named &lt;c&gt;explode&lt;/c&gt; to blow a string to pieces.
&lt;li&gt;
I have already known from my previous obfuscations that the power of unpack is easy to abuse: unpack can [id://933656|follow a linked list], [id://908680|generate the list of suffixes of a string], [id://863110|aid in arithmetic over a finite field], or (Update) [id://835076|drive a quine]. 
&lt;li&gt;
The evolution step on the whole board is implemented as just two calls to unpack and two calls to pack.  Is it possible to use even fewer calls?  
&lt;/ol&gt;
&lt;/readmore&gt;
</field>
</data>
</node>
