Code that runs the Game of Life on itself

43

14

Write two rectangular blocks of code, each w characters wide and h characters tall, that implement Conway's Game of Life when arranged in a grid. (w and h may be any positive integers)

For example, the two code blocks might be: (w = 3, h = 2, not actual code)

XX|
--+

and

  |
--+

Treat the first block as if it is the "alive" cell in the Game of Life and the second block as if it is the "dead" cell.

Now arrange multiple copies of these two blocks into a larger program that represents a Game of Life grid, such as this glider:

  |  |  |  |  |
--+--+--+--+--+
  |  |XX|  |  |
--+--+--+--+--+
  |  |  |XX|  |
--+--+--+--+--+
  |XX|XX|XX|  |
--+--+--+--+--+
  |  |  |  |  |
--+--+--+--+--+
  |  |  |  |  |
--+--+--+--+--+

Now here is the key point: when this code is run, the output must be the Game of Life grid that is the generation after this, using the same alive and dead code blocks for the cells.

So the output to the program above would be the next generation of the glider, which also serves as a runnable program:

  |  |  |  |  |
--+--+--+--+--+
  |  |  |  |  |
--+--+--+--+--+
  |XX|  |XX|  |
--+--+--+--+--+
  |  |XX|XX|  |
--+--+--+--+--+
  |  |XX|  |  |
--+--+--+--+--+
  |  |  |  |  |
--+--+--+--+--+

Running this would produce the next generation:

  |  |  |  |  |
--+--+--+--+--+
  |  |  |  |  |
--+--+--+--+--+
  |  |  |XX|  |
--+--+--+--+--+
  |XX|  |XX|  |
--+--+--+--+--+
  |  |XX|XX|  |
--+--+--+--+--+
  |  |  |  |  |
--+--+--+--+--+

And so on.

The process should be indefinitely repeatable and work for any arrangement of your alive and dead code blocks in a Game of Life grid.

The output grid should have the same the same dimensions as the program that it came from (5 by 6 above). Grid sizes as small as 1 by 1 should work, and they may be arbitrarily large. Naturally, an empty grid will just output itself.

Update: Being able to simulate grids of any dimensions without having to change the "tiles" would be ideal but since this seems very difficult I will accept answers that assume the grid is a certain size (maybe around 16*16).

The Game of Life rules are as follows:

  1. Any live cell with less than 2 or more than 3 live (Moore) neighbors dies.
  2. Any dead cell with exactly 3 live neighbors comes alive.
  3. Other cells do not change.

Scoring

The challenge is to do this in the smallest code block area possible. Your score is w * h. The lowest score wins. In case of ties the highest voted answer wins.

Details

  • Besides the newlines needed to make things rectangular, your two code blocks (and therefore your Game of Life grid programs) should only contain printable ASCII characters (hex codes 20 to 7E, no tabs, no extra newlines).
  • Both code blocks must have exactly w * h characters (besides the necessary newlines) with at least a one character difference. Padding them with spaces or comments is fine. They should not change from one generation to the next.
  • The output should either go to a file or to stdout. There is no input.
  • The boundary conditions may either be periodic (where the top edge borders the bottom and the left edge borders the right) or infinite (where all out-of-bounds cells are always dead). Choose whatever suits you best.
  • Reading your own source code is not allowed.
  • Any language or tool that already does this is not allowed. (Just in case one exists.)

Note: The -|+ characters above were only used to emphasize the cell borders. Your blocks do not need any distinguishable border. However, when displaying your output it would be helpful if there was some distinguishable separation between blocks, perhaps made with a character never used in the program so it is easy to remove. e.g.

DD+DD+DD+DD
+++++++++++
AA+DD+AA+DD

instead of

DDDDDDDD
AADDAADD

Calvin's Hobbies

Posted 2014-08-09T15:42:35.327

Reputation: 84 000

1

Here's a solution in Conway's Game of Life: http://radicaleye.com/lifepage/patterns/unitcell/ucdesc.html

– mbomb007 – 2015-08-21T21:08:08.320

@mbomb007 I didn't think of that, but about the OTCA metapixel – MilkyWay90 – 2019-01-26T04:19:43.860

I've also added them to the tag wiki now, so that in future one can simply copy it out of there. Feel free to improve the wording of that. – Martin Ender – 2014-08-09T17:53:34.367

"Any language or tool that already does this is not allowed. (Just in case one exists.)" - Darn, I was going to post an answer in Game of Life. Edit: Heck, for fun. – seequ – 2014-08-09T20:28:29.987

1this seems like a challenge suited to befunge or ><>. where is that guy that always has answers in ><>? – proud haskeller – 2014-08-09T22:47:32.027

Are magic macros such as __LINE__ allowed? – Martin Ender – 2014-08-11T14:06:17.820

1@MartinBüttner I'll say that's fine, as long it's nothing like __SOURCE_CODE__. – Calvin's Hobbies – 2014-08-11T14:14:42.823

2@proudhaskeller I think that's me and I'm already working on one in ><> :) — it's HARD. – tomsmeding – 2014-08-11T15:32:00.193

2Is the size of the arena allowed to be hardcoded or must the program work this out for itself? That is, does the code need to work in an arbitrary size arena? – trichoplax – 2014-08-11T19:55:56.077

1@githubphagocyte Ideally it would work in an arbitrary arena, but I may loosen that constraint since it's very hard to implement. – Calvin's Hobbies – 2014-08-11T23:00:17.777

From the discussion happening in chat it sounds like there will be at least one entry not requiring to know its arena size - if it's possible then keep it strict... – trichoplax – 2014-08-11T23:25:15.527

Im really curious if someone manages a correct solution with h>1. – Thaylon – 2014-08-19T11:49:53.107

You would get much nicer tiles, if the scoring function were width + height, instead of the product. I am currently working on a python solution with many lines, but also a certain width. It won't be able to compete with the Perl solution. – M.Herzkamp – 2014-08-21T15:23:18.010

@M.Herzkamp It's w*h because that's representative how many characters there are (which is why it's tagged [tag:code-golf]) – Calvin's Hobbies – 2014-08-21T23:51:03.440

Answers

12

Perl, w*h=233*1=233 (per cell)

Cells work in any grid size (actually even with irregular row length). Out of boundary cells are considered dead.

Dead Cell

my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=0}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});

Living Cell

my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=1}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});

Glider

my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=0}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=0}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=0}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=0}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=0}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});
my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=0}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=0}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=0}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=0}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=0}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});
my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=0}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=1}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=0}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=1}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=0}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});
my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=0}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=0}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=1}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=1}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=0}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});
my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=0}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=0}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=1}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=0}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=0}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});
my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=0}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=0}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=0}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=0}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=0}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});

generates:

my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=0}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=0}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=0}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=0}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=0}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});
my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=0}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=0}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=0}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=0}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=0}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});
my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=0}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=0}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=0}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=1}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=0}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});
my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=0}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=1}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=0}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=1}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=0}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});
my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=0}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=0}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=1}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=1}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=0}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});
my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=0}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=0}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=0}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=0}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});my$y;INIT{$y=__LINE__;$h{++$x{$y}}{$y}=0}eval($c=q{$n=0;$o++;for$i(-1..1){$n+=$i|$_&&$h{$o+$i}{$y+$_}for(-1..1)}print"my\$y;INIT{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=",$n==2?$h{$o}{$y}:$n==3||0,"}eval(\$c=q{$c});",!($o%=$x{$y})&&"\n"});

Explanation (not current)

# Perl has several special blocks that are executed at certain stages.
# BEGIN blocks are executed at compilation stage
BEGIN
{
  # current row index and row count in execution stage
  $y=__LINE__;
  # $x{$y} is the cell index in current row and cell count in execution stage
  # $h{..}{..} set cell to dead || alive
  $h{++$x{$y}}{$y}=1
}

# dead || alive
$v=1;
# row index
$p=__LINE__;
# write cell code to $c
$c=q{
  # reset neighbour count
  $n=0;
  # cell index
  $o++;
  # count living neighbours
  for$i(-1..1){$n+=$i|$_?$h{$o+$i}{$p+$_}:0for(-1..1)}
  # reset cell index to 0 when end of row is reached
  $o%=$x{$p};
  # dead || alive for next generation of this cell
  $v=$n==3||$n==2&&$v||0;
  # print the new cell
  print"BEGIN{\$y=__LINE__;\$h{++\$x{\$y}}{\$y}=$v}\$v=$v;\$p=__LINE__;\$c=q{$c};eval\$c;",$o?"":"\n"
};
# execute cell code
eval$c;

Perl, w*h=140*2=280 (per cell)

Multi-line cell is not efficient but can eliminate __LINE__.

Dead Cell

BEGIN{++$v}eval($c=q<;;;;;;++$y;map{$i{$y}+=$h{$y+$_}}(-$a-1..-$a+1,-1,1,$a-1..$a+1);print'BEGIN{++$v}eval($c=q<',$c,'>);',!($y%$a)&&"\n">);
BEGIN{$a||=$v,$h{++$x}=0}eval($c=q<$n=$i{++$z};print'BEGIN{$a||=$v,$h{++$x}=',$n==2?$h{$z}:$n==3||0,'}eval($c=q<',$c,'>);',!($z-$y)&&"\n">);

Living Cell

BEGIN{++$v}eval($c=q<;;;;;;++$y;map{$i{$y}+=$h{$y+$_}}(-$a-1..-$a+1,-1,1,$a-1..$a+1);print'BEGIN{++$v}eval($c=q<',$c,'>);',!($y%$a)&&"\n">);
BEGIN{$a||=$v,$h{++$x}=1}eval($c=q<$n=$i{++$z};print'BEGIN{$a||=$v,$h{++$x}=',$n==2?$h{$z}:$n==3||0,'}eval($c=q<',$c,'>);',!($z-$y)&&"\n">);

Thaylon

Posted 2014-08-09T15:42:35.327

Reputation: 1 324

8

JavaScript ES6 (239 x 1 characters per cell)

Dead cell:

f=e=>{S=S||[],j=S.length,S[j]=0,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');

Alive cell:

f=e=>{S=S||[],j=S.length,S[j]=1,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');

Only difference being S[j]=0 for dead and S[j]=1 for alive cells.

Grid size is controlled with variables W (width) and H (height). Cells outside grid are dead.

Commented version

f=r=>{
    S=S||[],    // cell states
    j=S.length, // current index
    S[j]=1,     // state of this cell
    W=5,        // width of grid
    H=6;        // height of grid
    if (j+2 > W*H)  // if last cell
        /* Loop all cells and gather code for output */
        for (i in S)
            /* Function source with state replaced */
            r +=("f="+f+";f('');").replace(/0|1/, ~~[S[i],1] // lookup table
                [~~S[i-W] + ~~S[+i+W] + // +i to convert from int to string
                ~~(i % W ? S[--i] + S[i-W] + S[W+i++] : 0) +
                ~~(i % W < W-1 ? S[++i] + S[i-W] + S[W+i--] : 0)-2]),
            r = ++i % W ? r : (console.log(r), "")
};
f(''); // call current cell function to set state of the cell

Glider (5 x 6 grid)

f=e=>{S=S||[],j=S.length,S[j]=0,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');f=e=>{S=S||[],j=S.length,S[j]=0,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');f=e=>{S=S||[],j=S.length,S[j]=0,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');f=e=>{S=S||[],j=S.length,S[j]=0,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');f=e=>{S=S||[],j=S.length,S[j]=0,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');
f=e=>{S=S||[],j=S.length,S[j]=0,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');f=e=>{S=S||[],j=S.length,S[j]=0,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');f=e=>{S=S||[],j=S.length,S[j]=1,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');f=e=>{S=S||[],j=S.length,S[j]=0,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');f=e=>{S=S||[],j=S.length,S[j]=0,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');
f=e=>{S=S||[],j=S.length,S[j]=0,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');f=e=>{S=S||[],j=S.length,S[j]=0,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');f=e=>{S=S||[],j=S.length,S[j]=0,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');f=e=>{S=S||[],j=S.length,S[j]=1,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');f=e=>{S=S||[],j=S.length,S[j]=0,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');
f=e=>{S=S||[],j=S.length,S[j]=0,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');f=e=>{S=S||[],j=S.length,S[j]=1,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');f=e=>{S=S||[],j=S.length,S[j]=1,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');f=e=>{S=S||[],j=S.length,S[j]=1,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');f=e=>{S=S||[],j=S.length,S[j]=0,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');
f=e=>{S=S||[],j=S.length,S[j]=0,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');f=e=>{S=S||[],j=S.length,S[j]=0,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');f=e=>{S=S||[],j=S.length,S[j]=0,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');f=e=>{S=S||[],j=S.length,S[j]=0,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');f=e=>{S=S||[],j=S.length,S[j]=0,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');
f=e=>{S=S||[],j=S.length,S[j]=0,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');f=e=>{S=S||[],j=S.length,S[j]=0,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');f=e=>{S=S||[],j=S.length,S[j]=0,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');f=e=>{S=S||[],j=S.length,S[j]=0,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');f=e=>{S=S||[],j=S.length,S[j]=0,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');

Outputs next generation:

f=e=>{S=S||[],j=S.length,S[j]=0,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');f=e=>{S=S||[],j=S.length,S[j]=0,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');f=e=>{S=S||[],j=S.length,S[j]=0,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');f=e=>{S=S||[],j=S.length,S[j]=0,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');f=e=>{S=S||[],j=S.length,S[j]=0,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');
f=e=>{S=S||[],j=S.length,S[j]=0,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');f=e=>{S=S||[],j=S.length,S[j]=0,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');f=e=>{S=S||[],j=S.length,S[j]=0,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');f=e=>{S=S||[],j=S.length,S[j]=0,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');f=e=>{S=S||[],j=S.length,S[j]=0,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');
f=e=>{S=S||[],j=S.length,S[j]=0,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');f=e=>{S=S||[],j=S.length,S[j]=1,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');f=e=>{S=S||[],j=S.length,S[j]=0,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');f=e=>{S=S||[],j=S.length,S[j]=1,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');f=e=>{S=S||[],j=S.length,S[j]=0,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');
f=e=>{S=S||[],j=S.length,S[j]=0,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');f=e=>{S=S||[],j=S.length,S[j]=0,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');f=e=>{S=S||[],j=S.length,S[j]=1,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');f=e=>{S=S||[],j=S.length,S[j]=1,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');f=e=>{S=S||[],j=S.length,S[j]=0,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');
f=e=>{S=S||[],j=S.length,S[j]=0,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');f=e=>{S=S||[],j=S.length,S[j]=0,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');f=e=>{S=S||[],j=S.length,S[j]=1,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');f=e=>{S=S||[],j=S.length,S[j]=0,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');f=e=>{S=S||[],j=S.length,S[j]=0,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');
f=e=>{S=S||[],j=S.length,S[j]=0,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');f=e=>{S=S||[],j=S.length,S[j]=0,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');f=e=>{S=S||[],j=S.length,S[j]=0,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');f=e=>{S=S||[],j=S.length,S[j]=0,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');f=e=>{S=S||[],j=S.length,S[j]=0,W=5,H=6;if(j+2>W*H)for(i in S)e+=("f="+f+";f('');").replace(/0|1/,~~[S[i],1][~~S[i-W]+~~S[+i+W]+~~(i%W?S[--i]+S[i-W]+S[W+i++]:0)+~~(i%W<W-1?S[++i]+S[i-W]+S[W+i--]:0)-2]),e=++i%W?e:(console.log(e),"")};f('');

Mika Lammi

Posted 2014-08-09T15:42:35.327

Reputation: 1 151

@mbomb007 No. | is bitwise operator and works only with numbers. || is logical operator that returns left side if it's "trueish", right side otherwise. – Mika Lammi – 2015-08-24T06:54:08.437

The 's==0&&' in '(v<2||v>3?0:s==0&&v==3?1:s)' seems to be redundant. – Thaylon – 2014-08-19T14:41:55.607

@Thaylon Thanks for comment, I will check that out. I am currently making several improvements. Looks like next version is going to be ~300 characters. – Mika Lammi – 2014-08-19T15:22:52.340

5

Python, 67x33 = 2,211

Note, that this is my pretty version, where I added a line of "#" to every other line in a life cell, and " " to every other line in a dead cell. Also, the cells are separated by "#" lines. This way, you can place an initial configuration in a text file, e.g. GOL.py, and look at it with a tiny font to see what is going on each step. To get to the next step, execute python GOL.py > GOL1.py;mv GOL1.py GOL.py and have a look again.

My solution works on a grid of arbitrary size and assumes periodic boundary conditions. I haven't tested varying line widths, but there definitely is capability for that.

I should also mention, that the success of this solution depends on __del__ being called when a is destroyed at the end of the program. The Python documentation states

It is not guaranteed that __del__() methods are called for objects that still exist when the interpreter exits.

So, I just hope, that this not only works with my interpreter.

Live cell:

c=['import sys;o=sys.stdout.write;r=range;l=len;w=66',#############
###################################################################
'class B(list):',' def __getitem__(s,k):',#########################
###################################################################
'  return list.__getitem__(s,k%l(s))','class A(B):',###############
###################################################################
' def e(s,k,m):','  g=(s[k-1][m]+s[k+1][m]+s[k][m-1]+s[k][m+1]',###
###################################################################
'+s[k-1][m-1]+s[k-1][m+1]+s[k+1][m-1]+s[k+1][m+1])',###############
###################################################################
'  if s[k][m]==0:return 1 if g==3 else 0',#########################
###################################################################
'  return 1 if g==2 or g==3 else 0',' def u(s,i,y,b):',############
###################################################################
'  for j in r(l(s[i])):o(y+b[s.e(i,j)]*(w-l(y))+"#")',#############
###################################################################
'  print;print"#".join([b[s.e(i,j)]*w for j in r(l(s[i]))])+"#"',##
###################################################################
' def __del__(s):','  for i in r(l(s)):','   y="";b=[" ","#"]',####
###################################################################
'   for q in ["c=["]+[repr(x)+","for x in c]+[',###################
###################################################################
'"];import os;exec(os.linesep.join(c))"]:',########################
###################################################################
'    if l(y+q)>w:s.u(i,y,b);y=""','    y+=q','   s.u(i,y,b)',######
###################################################################
'   for j in r(l(s[i])):o("a[-1].append(%i);"%s.e(i,j)+" "*51)',###
###################################################################
'   print;print "#"*l(s[i])*(w+1)','if "a" not in locals():a=A()',#
###################################################################
'a.append(B())',];import os;exec(os.linesep.join(c))###############
###################################################################
a[-1].append(1);                                                   
###################################################################

Dead cell:

c=['import sys;o=sys.stdout.write;r=range;l=len;w=66',            #
                                                                  #
'class B(list):',' def __getitem__(s,k):',                        #
                                                                  #
'  return list.__getitem__(s,k%l(s))','class A(B):',              #
                                                                  #
' def e(s,k,m):','  g=(s[k-1][m]+s[k+1][m]+s[k][m-1]+s[k][m+1]',  #
                                                                  #
'+s[k-1][m-1]+s[k-1][m+1]+s[k+1][m-1]+s[k+1][m+1])',              #
                                                                  #
'  if s[k][m]==0:return 1 if g==3 else 0',                        #
                                                                  #
'  return 1 if g==2 or g==3 else 0',' def u(s,i,y,b):',           #
                                                                  #
'  for j in r(l(s[i])):o(y+b[s.e(i,j)]*(w-l(y))+"#")',            #
                                                                  #
'  print;print"#".join([b[s.e(i,j)]*w for j in r(l(s[i]))])+"#"', #
                                                                  #
' def __del__(s):','  for i in r(l(s)):','   y="";b=[" ","#"]',   #
                                                                  #
'   for q in ["c=["]+[repr(x)+","for x in c]+[',                  #
                                                                  #
'"];import os;exec(os.linesep.join(c))"]:',                       #
                                                                  #
'    if l(y+q)>w:s.u(i,y,b);y=""','    y+=q','   s.u(i,y,b)',     #
                                                                  #
'   for j in r(l(s[i])):o("a[-1].append(%i);"%s.e(i,j)+" "*51)',  #
                                                                  #
'   print;print "#"*l(s[i])*(w+1)','if "a" not in locals():a=A()',#
                                                                  #
'a.append(B())',];import os;exec(os.linesep.join(c))              #
                                                                  #
a[-1].append(0);                                                   
###################################################################

Python, 67x17 = 1,139 To get a golfed version, still with some way to recognise life cells from afar, the second print statement in lines 19 and 31 as well as every other line is deleted.

Live cell:

c=['import sys;o=sys.stdout.write;r=range;l=len;w=66',#############
'class B(list):',' def __getitem__(s,k):',#########################
'  return list.__getitem__(s,k%l(s))','class A(B):',###############
' def e(s,k,m):','  g=(s[k-1][m]+s[k+1][m]+s[k][m-1]+s[k][m+1]',###
'+s[k-1][m-1]+s[k-1][m+1]+s[k+1][m-1]+s[k+1][m+1])',###############
'  if s[k][m]==0: return 1 if g==3 else 0',########################
'  return 1 if g==2 or g==3 else 0',' def u(s,i,y,b):',############
'  for j in r(l(s[i])):o(y+b[s.e(i,j)]*(w-l(y))+"#")','  print',###
' def __del__(s):','  for i in r(l(s)):','   y="";b=[" ","#"]',####
'   for q in ["c=["]+[repr(x)+","for x in c]+[',###################
'"];import os;exec(os.linesep.join(c))"]:',########################
'    if l(y+q)>w:s.u(i,y,b);y=""','    y+=q','   s.u(i,y,b)',######
'   for j in r(l(s[i])):o("a[-1].append(%i);"%s.e(i,j)+" "*51)',###
'   print','if "a" not in locals():a=A()','a.append(B())',#########
];import os;exec(os.linesep.join(c))###############################
a[-1].append(1);                                                   

Dead cell:

c=['import sys;o=sys.stdout.write;r=range;l=len;w=66',            #
'class B(list):',' def __getitem__(s,k):',                        #
'  return list.__getitem__(s,k%l(s))','class A(B):',              #
' def e(s,k,m):','  g=(s[k-1][m]+s[k+1][m]+s[k][m-1]+s[k][m+1]',  #
'+s[k-1][m-1]+s[k-1][m+1]+s[k+1][m-1]+s[k+1][m+1])',              #
'  if s[k][m]==0: return 1 if g==3 else 0',                       #
'  return 1 if g==2 or g==3 else 0',' def u(s,i,y,b):',           #
'  for j in r(l(s[i])):o(y+b[s.e(i,j)]*(w-l(y))+"#")','  print',  #
' def __del__(s):','  for i in r(l(s)):','   y="";b=[" ","#"]',   #
'   for q in ["c=["]+[repr(x)+","for x in c]+[',                  #
'"];import os;exec(os.linesep.join(c))"]:',                       #
'    if l(y+q)>w:s.u(i,y,b);y=""','    y+=q','   s.u(i,y,b)',     #
'   for j in r(l(s[i])):o("a[-1].append(%i);"%s.e(i,j)+" "*51)',  #
'   print','if "a" not in locals():a=A()','a.append(B())',        #
];import os;exec(os.linesep.join(c))                              #
a[-1].append(0);                                                   

If there is demand, I will gladly explain the inner workings of my solution when I find the time.

M.Herzkamp

Posted 2014-08-09T15:42:35.327

Reputation: 1 227

I tried using <sup><sub>...</sub></sup> within a <pre><code> block. This doesn't decrease the height of a line, but the font of the text is decreased for each occurrence. Using only subscript or only superscript will make the code float down or up outside of the markup, but alternating will keep it centered vertically. – mbomb007 – 2016-12-14T15:45:32.013

@mbomb007 It seems you found a way after all ;-) – M.Herzkamp – 2016-12-14T15:45:36.703

Yeah, I think it helps the smaller program look like a square, at least. – mbomb007 – 2016-12-14T15:46:16.607

I think it'd be nice if the golfed version was at the top of the answer, with its score listed in the header. Also, I think you could golf it more. – mbomb007 – 2017-04-12T14:24:13.197

I put it in this order because I thought the ungolfed version more in spirit of the question (the score is mediocre at best anyways). Btw. @mbomb007 I begin to suspect that you put more time into this answer than me :P Is there any way to share the score? – M.Herzkamp – 2017-04-18T09:08:10.427