Created
February 20, 2017 22:45
-
-
Save joeltg/dff64c2ccf3aa9f74f716b1e0bceae74 to your computer and use it in GitHub Desktop.
automaton
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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