Skip to content

Instantly share code, notes, and snippets.

@vitapluvia
Created November 18, 2016 02:29
Show Gist options
  • Save vitapluvia/bf174a3197071f71beb36ce6fc073289 to your computer and use it in GitHub Desktop.
Save vitapluvia/bf174a3197071f71beb36ce6fc073289 to your computer and use it in GitHub Desktop.
Cellular Automata Characters
#!/usr/bin/env python
import string
import sys
def setup(rowSize, colSize):
mid = colSize / 2
board = [[0 for _ in range(colSize)] for __ in range(rowSize)]
board[0][mid] = 1
return board
def printRow(board, y, space, char):
for col in board[y]:
sys.stdout.write(char) if col == 1 else sys.stdout.write(space)
def printCharRow(board, y, space, char):
bits = ''.join([str(x) for x in board[y]])
bits = hex(int('0b{}'.format(bits), 2))
bits = bits.replace('L', '')
bits = bits.lstrip('0x')
bits = bits.rstrip('0')
try:
bits = bits.decode('hex')
except:
bits += '0'
bits = bits.decode('hex')
#strChars = string.printable
strChars = string.lowercase + string.uppercase
bits = ''.join([strChars[int(ord(bits[x]) + y * x) % len(strChars)] for x in range(len(bits))])
sys.stdout.write('{}'.format(bits))
def setRow(board, x, y, rows, cols, rule):
left = board[y-1][(x-1) % cols]
mid = board[y-1][x]
right = board[y-1][(x+1) % cols]
bits = int('0b{}{}{}'.format(left, mid, right), 2)
ruleBits = [int(b) for b in '{0:08b}'.format(rule)][::-1]
for i, r in enumerate(ruleBits):
if r == 1 and bits == i:
board[y][x] = 1
def main():
ROWS = 1128
COLS = 1128
DEFAULT = 118
SPACE = " "
CHAR = "."
rule = int(sys.argv[1]) if len(sys.argv) >= 2 else DEFAULT
board = setup(ROWS, COLS)
for y in range(ROWS):
for x in range(COLS):
setRow(board, x, y, ROWS, COLS, rule)
#printRow(board, y, SPACE, CHAR)
printCharRow(board, y, SPACE, CHAR)
sys.stdout.write('\n')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment