Skip to content

Instantly share code, notes, and snippets.

@sneeu
Created February 17, 2011 13:56
Show Gist options
  • Save sneeu/831760 to your computer and use it in GitHub Desktop.
Save sneeu/831760 to your computer and use it in GitHub Desktop.
A somewhat untidy Python script to generate a 1d cellular automata based on an MD5 hash.
"""
Example output: http://www.flickr.com/photos/sneeu/5448646680/
"""
import hashlib
RULES = {
4: {
(0, 0, 0, ): 0,
(0, 0, 1, ): 1,
(0, 1, 0, ): 1,
(0, 1, 1, ): 1,
(1, 0, 0, ): 0,
(1, 0, 1, ): 0,
(1, 1, 0, ): 1,
(1, 1, 1, ): 1,
},
107: {
(0, 0, 0, ): 1,
(0, 0, 1, ): 0,
(0, 1, 0, ): 0,
(0, 1, 1, ): 1,
(1, 0, 0, ): 0,
(1, 0, 1, ): 1,
(1, 1, 0, ): 0,
(1, 1, 1, ): 0,
}
}
def rule(initial, rule=4, odd=0):
ret = []
for i, c in enumerate(initial):
l = r = odd
if i > 0:
l = initial[i - 1]
if (i + 1) < len(initial):
r = initial[i + 1]
n = RULES[rule][(l, c, r)]
ret.append(n)
return ret
def rows(initial, iterations):
r = [initial]
for i in xrange(0, iterations):
initial = r[-1]
r.append(rule(initial, 107, i % 2))
return r
def main():
initial = []
n = int(hashlib.md5('[email protected]').hexdigest(), 16)
power = 1
while power <= n:
initial.insert(0, n & power and 1 or 0)
power = power * 2
initial = [0] * 50 + initial + [0] * 50
for r in rows(initial, 30):
print ''.join([c and '#' or ' ' for c in r])
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment