From 04593730a456e280d00d7ee551c67e6c0b9e2ffa Mon Sep 17 00:00:00 2001 From: Sanky Date: Wed, 17 Oct 2012 08:52:18 +0200 Subject: [PATCH] cellular.py: a simple 2D cellular automata implementation --- host_python/cellular.py | 44 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 host_python/cellular.py diff --git a/host_python/cellular.py b/host_python/cellular.py new file mode 100644 index 0000000..3fa2802 --- /dev/null +++ b/host_python/cellular.py @@ -0,0 +1,44 @@ +#!/usr/bin/python + +import sys + +from ledbar import Ledbar + +PIXELS = 20 + +rules = {(1,1,1): 0, + (1,1,0): 0, + (1,0,1): 0, + (1,0,0): 1, + (0,1,1): 1, + (0,1,0): 1, + (0,0,1): 1, + (0,0,0): 0} + +iteration = [0]*PIXELS +iteration[PIXELS/2] = 1 + +def iterate(iteration): + new = [] + for i in range(len(iteration)): + if 0 < i < PIXELS-1: + top = (iteration[i-1], iteration[i], iteration[i+1]) + new.append(rules[top]) + else: + new.append(0) + return new + +def update(i): + return (iteration[i], iteration[i], iteration[i]) + +l = Ledbar(PIXELS) +work = True +t = 0 +while work: + for i in xrange(PIXELS): + c = update(i) + l.set_pixel(i, c[0], c[1], c[2]) + work = l.update() + t += 1 + if not (t % 50): + iteration = iterate(iteration)