One-Dimensional Cellular Automata

Summary

This is an example from my Systems and Software Portfolio. This is a quick cellular automata simulator I wrote in perl for one-dimensional models, specifically to explore the Turing-complete Rule 110.

Simulator

#!/usr/bin/perl -w
use strict;

### 
# One Dimensional Cellular Automata Simulation
#   Copyright (c) 2007 by Katherine Doubek.
###
# Syntax: ./1dca.pl <rule number>
#   Where <rule number> is an integer between 0 and 255
#     Will run a 72x36 simulation.
###

my $rule = $ARGV[0] || die "Syntax: ./1dca.pl <rule number>\n";


### Initialize the rows
my $thisrow = [];
my $thatrow = [];
push @$thisrow, ' ' foreach 0..71; ### The current row
push @$thatrow, ' ' foreach 0..71; ### The next row
$$thisrow[36] = '#';

### Loop it.  Loop it good.
foreach my $row (0..35){

	foreach my $column (0..71){

		### Figure out the three root positions
		my $posLeft= ($$thisrow[$column-1] eq '#'?1:0);
		my $posMid = ($$thisrow[$column] eq '#'?1:0);
		my $posRight;
		if($column == 71){
			$posRight = 0;	
		}else{
			$posRight = ($$thisrow[$column+1] eq '#'?1:0);
		}
		
		### Figure out the next row
		if( $posLeft &&  $posMid &&  $posRight){
			$$thatrow[$column] = ($rule & 128?'#':' ');
		}
		if( $posLeft &&  $posMid && !$posRight){
			$$thatrow[$column] = ($rule & 64?'#':' ');
		}
		if( $posLeft && !$posMid &&  $posRight){
			$$thatrow[$column] = ($rule & 32?'#':' ');
		}
		if( $posLeft && !$posMid && !$posRight){
			$$thatrow[$column] = ($rule & 16?'#':' ');
		}
		if(!$posLeft &&  $posMid &&  $posRight){
			$$thatrow[$column] = ($rule & 8?'#':' ');
		}
		if(!$posLeft &&  $posMid && !$posRight){
			$$thatrow[$column] = ($rule & 4?'#':' ');
		}
		if(!$posLeft && !$posMid &&  $posRight){
			$$thatrow[$column] = ($rule & 2?'#':' ');
		}
		if(!$posLeft && !$posMid && !$posRight){
			$$thatrow[$column] = ($rule & 1?'#':' ');
		}
	}
		
	### Print the row
	print(sprintf("%3d",$row)." ".join('',@$thisrow)."\n");
	
	### Swap the rows
	my $tempPtr = $thisrow;
	$thisrow = $thatrow;
	$thatrow = $tempPtr;

}

Sample Output — Rule 110

  1                                    ##                                   
  2                                   ###                                   
  3                                  ## #                                   
  4                                 #####                                   
  5                                ##   #                                   
  6                               ###  ##                                   
  7                              ## # ###                                   
  8                             ####### #                                   
  9                            ##     ###                                   
 10                           ###    ## #                                   
 11                          ## #   #####                                   
 12                         #####  ##   #                                   
 13                        ##   # ###  ##                                   
 14                       ###  #### # ###                                   
 15                      ## # ##  ##### #                                   
 16                     ######## ##   ###                                   
 17                    ##      ####  ## #                                   
 18                   ###     ##  # #####                                   
 19                  ## #    ### ####   #                                   
 20                 #####   ## ###  #  ##                                   
 21                ##   #  ##### # ## ###                                   
 22               ###  ## ##   ######## #                                   
 23              ## # ######  ##      ###                                   
 24             #######    # ###     ## #                                   
 25            ##     #   #### #    #####                                   
 26           ###    ##  ##  ###   ##   #                                   
 27          ## #   ### ### ## #  ###  ##                                   
 28         #####  ## ### ###### ## # ###                                   
 29        ##   # ##### ###    ######## #                                   
 30       ###  ####   ### #   ##      ###                                   
 31      ## # ##  #  ## ###  ###     ## #                                   
 32     ######## ## ##### # ## #    #####                                   
 33    ##      ######   ########   ##   #                                   
 34   ###     ##    #  ##      #  ###  ##                                   
 35  ## #    ###   ## ###     ## ## # ###   
« Back to the Systems and Software Portfolio