Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from sys import stdin
- EverythingInExistence = [] # doesn't do anything, just a comment in the form of code
- def bootstrap(): # this is for you
- print("Please input an integer value for x\n")
- x = int(stdin.read())
- print("Please input an integer value for y\n"+"To escape your input, press [Ctrl+Z] + [Enter] (Windows) or [Ctrl+D] (Linux)")
- y = int(stdin.read())
- print("Please input the running time for the simulation.")
- T = int(stdin.read())
- print("Please input a nested list two deep containing the values you wish to simulate.\n"
- +"Here is an example of a glider:"
- +"[[0,0,1,0]\n"
- +" [1,0,1,0]\n"
- +" [0,1,1,0]\n")
- EverythingInExistence.append(Universe(x,y,prog))
- for t in range(T):
- for i in EverythingInExistence:
- i.update()
- class Universe: # a generic world
- def __init__(self, x, y, prog=[], devi=[]): # init the universe
- self.template = [[0 for i in range(y)] for j in range(x)] # this is just here in case i need it
- self.univ = [[0 for i in range(y)] for j in range(x)] # actually init the the universe
- self.timeBuffer = [[[None,0] for i in range(y)] for j in range(x)] # it's purpose isn't apparent, but it should be latter
- if prog and devi: # prog stands for progenitor, devi for deviation
- univ = [[ prog[i][j] + devi[i][j] for i in range(x)] for j in range(y)] # prog is the universe that called Universe(), devi is used to calculate what this universe will be
- elif prog:
- univ = [[prog[i][j] for i in range(x)] for j in range(y)
- def getNextState(self,x,y): # simple implementation of CGoL, with some deviations
- if x == self.x or x == 0 or y == self.y or y == 0: # this hack prevents exceptions related to sum depending on values that don't exist
- return 0
- sum = [sum(i) for i in univ[y-1:y+1] # generic sum of neighbors
- if sum = 3:
- return 1
- elif sum = 2 and (univ[x][y] = 1):
- return 1
- elif sum > 6: ## PAY ATTENTION, this where stuff starts to get interesting ##
- self.branch(sum - 6) ### In this case, neighbors greater than 6 cause time travel.
- ### This is arbitrary and not integral to the general system
- return 0 # 0 is returned because the branch creates a universe where 1 was returned in it's stead.
- else:
- return 0
- def update(self,univ): # global update
- temp = univ # in anticipation of a branch
- for i in range(y): # decrements the times on the buffer, unless they aren't carrying a value.
- for i in range(x):
- if not timeBuffer[x][y][0] == None:
- timeBuffer[x][y] = [timeBuffer[x][y][0], max(0, timeBuffer[x][y][1] -1)]
- self.univ = [[self.getNextState(i,j) for i in range(self.y)] for j in range(self.x)] # updates every cell
- if self.branches: # list containing cells based on whether or not the branch function was called
- when updating cells
- theRealBranch(self.timeBuffer)
- for i in range(y): # removes inconsistent timelines
- for j in range(x):
- if timeBuffer[x][y][1] == 0 and not timeBuffer[x][y][0] == univ[x][y]:
- EverythingInExistence.remove(self) # removes self
- branches = []
- def branch(self, self.sum): # doesn't actually branch, just lets theRealBranch function do it's job
- self.branches.append([x,y,sum - 6])
- self.timeBuffer[x][y] = (0, sum - 6)
- def theRealBranch(self, self.timeBuffer, self.branches): ### the Magnum Opus of this program, branches the universes
- creating = True
- while creating:
- counter = 0
- list = [0 for i in branches] # this list will iterate over all 2^len(branches) universes
- for i in list:
- tempTimeBuffer = self.timeBuffer # There are just temporary variables here
- tempDevi = self.template # while their universe is being created
- counter += 1 # keep track of where we are
- tempDevi[branches[counter][x]][branches[counter][1]] = list[counter] # this looks scary, bu it just take coordinates from branches and assigns
- them values based off list
- for i in branches: # updates timeBuffer to prevent weird stuff (tm) from happening in the new
- universe
- tempTimeBuffer[x][y] = [list[counter], branches[2]] #
- EverythingInExistence.append(Universe( self.x, self.y, self.univ, tempDevi) # indented to this to allow multiple creations
- myCreation = EverythingInExistence(len(EverythingInExistence)-1)
- myCreation.timeBuffer = tempTimeBuffer
- list[counter] += 1
- if list[counter] = 2:
- for i in range(list): # list is represented as a binary number, and incremented
- j = len(list) - i
- if list[j] = 2 and not j = 0: # j can't be 0 because there is an upper bound on the value of list, interpreted
- as a binary
- list[j] -= 1
- list[j-1]+=1
- elif j = 0 and list[j] = 2: # j has exceded it's max value
- creating = False # Let there be light
- break
- else:
- break
- else:
- pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement