Skip to content

Instantly share code, notes, and snippets.

@joeltg
Created February 20, 2017 22:45
Show Gist options
  • Save joeltg/dff64c2ccf3aa9f74f716b1e0bceae74 to your computer and use it in GitHub Desktop.
Save joeltg/dff64c2ccf3aa9f74f716b1e0bceae74 to your computer and use it in GitHub Desktop.
automaton
from functools import reduce
from operator import getitem
def bst(n, i):
if n % 2 == 0:
h = 2 ** (n / 2)
remainder, quotient = i % h, i // h
return bst(n / 2, remainder), bst(n / 2, quotient)
else:
return i
class Rule:
def __init__(self, rule):
self.bst = bst(8, rule)
def check(self, state):
return reduce(getitem, state, self.bst)
class Layer(list):
def __getitem__(self, key):
if isinstance(key, slice):
return [self.__getitem__(i) for i in range(key.start, key.stop)]
elif key >= 0 and key < len(self):
return list.__getitem__(self, key)
else:
return 0
first_layer = Layer([1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment