develooper Front page | perl.fwp | Postings from December 2001

It's Life, Jim.

From:
ianb
Date:
December 6, 2001 21:39
Subject:
It's Life, Jim.
Message ID:
20011207163629.W21941@ot.com.au
Fun-havers,

A while ago I posted this to perl-ai when the subject of game of life
came up. Simon Cozens then alerted me to the existence of Lord
Damian's SelfGOL, to which this program cannot be compared, but I
thought it might still count as "fun".

Yours faithfully abusing the regex engine,


Ian Boreham

---



#!/usr/bin/perl -w

$u =
      "                                                                                          \n".
      "                                                                                          \n".
      "                                                                                          \n".
      "                                                                                          \n".
      "                                                                                          \n".
      "                                                                                          \n".
      "                                                                                          \n".
      "          o  o   o   ooo  ooooo      ooo   o   o   ooo  ooooo  o   o ooooo ooo            \n".
      "          o  o   o  o       o       o   o  oo  o  o   o   o    o   o o     o  o           \n".
      "          o  o   o   ooo    o       ooooo  o o o  o   o   o    ooooo ooo   ooo            \n".
      "      o   o  o   o      o   o       o   o  o  oo  o   o   o    o   o o     o  o           \n".
      "       ooo    ooo    ooo    o       o   o  o   o   ooo    o    o   o ooooo o  o           \n".
      "                                                                                          \n".
      "                                                                                          \n".
      "      o      o  ooooo ooooo    o   o   ooo    ooo   o  o  ooooo  oooo                     \n".
      "      o      o  o     o        o   o  o   o  o   o  o o   o      o   o                    \n".
      "      o      o  ooo   ooo      ooooo  ooooo  o      oo    ooo    oooo                     \n".
      "      o      o  o     o        o   o  o   o  o   o  o o   o      o   o                    \n".
      "      ooooo  o  o     ooooo    o   o  o   o   ooo   o  o  ooooo  o   o                    \n".
      "                                                                                          \n".
      "                                                                                          \n".
      "                                                                                          \n".
      "                                                                                          \n".
      "                                                                                          \n".
      "                                                                                          \n".
      "                                                                                          \n".

    "\n";

$w = index $u, "\n";

%genes = (
	  "CA" => "(?<=(.)[\\w\\W]{" . ($w+1) . "})",
	  "A" => "(?<=(.)[\\w\\W]{" . ($w) . "})",
	  "GA" => "(?<=(.)[\\w\\W]{" . ($w-1) . "})",
	  "C" => "(?<=(.))",
	  "G" => "(?=(.))",
	  "TC" => "(?=[\\w\\W]{" . ($w-1) . "}(.))",
	  "T" => "(?=[\\w\\W]{" . ($w) . "}(.))",
	  "TG" => "(?=[\\w\\W]{" . ($w+1) . "}(.))",
	  );

($dna = "CAAGAC.GTCTTG|CAAGAC.G|C.GTCTTG|CAAC.TCT|AGA.GTTG|CAAC.|AGA.G|C.TCT|.GTTG")
    =~ s/CA|GA|A|TC|TG|T|A|C|G|T/$genes{$&}/g;

sub evolve
{
    my ($n);
    $n += (defined($$_) and $$_ eq "o")? 1: 0 for (1 .. 42);
    return (($n == 2 and $& eq "o") or $n == 3)? "o": " ";
}

print($u), $u =~ s/$dna/evolve/ego while("universe exists");




nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About