Skip to content

Instantly share code, notes, and snippets.

@tyadon
Created April 21, 2014 16:08
Show Gist options
  • Save tyadon/11147268 to your computer and use it in GitHub Desktop.
Save tyadon/11147268 to your computer and use it in GitHub Desktop.
def getCell(ruleList, left, right, mid):
bin = str(left) + str(mid) + str(right)
cell = 0
if bin == "000":
cell = ruleList[0]
elif bin == "001":
cell = ruleList[1]
elif bin == "010":
cell = ruleList[2]
elif bin == "011":
cell = ruleList[3]
elif bin == "100":
cell = ruleList[4]
elif bin == "101":
cell = ruleList[5]
elif bin == "110":
cell = ruleList[6]
elif bin == "111":
cell = ruleList[7]
return cell
def getRow(prevRow, ruleList):
row = []
for i in range(0, len(prevRow)):
left = prevRow[i-1] if (i-1 > 0) else 0
right = prevRow[i+1] if (i+1 < len(prevRow)) else 0
mid = prevRow[i]
row.append(getCell(ruleList, left, right, mid))
printRow(row)
return row
def printRow(row):
for i in range(0, len(row)):
print(str(row[i]), end="")
print("")
def main(rule, steps):
print("P1 " + str(steps*2+1), str(steps+1))
# Create rule list
ruleList = [0] * 8
ruleList[0] = 1 if (rule & 1 == 1) else 0
ruleList[1] = 1 if (rule & 2 == 2) else 0
ruleList[2] = 1 if (rule & 4 == 4) else 0
ruleList[3] = 1 if (rule & 8 == 8) else 0
ruleList[4] = 1 if (rule & 16 == 16) else 0
ruleList[5] = 1 if (rule & 32 == 32) else 0
ruleList[6] = 1 if (rule & 64 == 64) else 0
ruleList[7] = 1 if (rule & 128 == 128) else 0
# Create first row
prevRow = [0] * (steps * 2 + 1)
prevRow[steps] = 1
printRow(prevRow)
for i in range(0, steps):
newRow = getRow(prevRow, ruleList)
prevRow = newRow
main(105, 200)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment