• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# CGoL with Time Travel

Altiumbe Aug 12th, 2015 (edited) 288 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. from sys import stdin
2.
3. EverythingInExistence = []                                                        # doesn't do anything, just a comment in the form of code
4.
5. def bootstrap():                                                                # this is for you
6.     print("Please input an integer value for x\n")
8.     print("Please input an integer value for y\n"+"To escape your input, press [Ctrl+Z] + [Enter] (Windows) or [Ctrl+D] (Linux)")
10.     print("Please input the running time for the simulation.")
12.     print("Please input a nested list two deep containing the values you wish to simulate.\n"
13.     +"Here is an example of a glider:"
14.     +"[[0,0,1,0]\n"
15.     +" [1,0,1,0]\n"
16.     +" [0,1,1,0]\n")
17.     EverythingInExistence.append(Universe(x,y,prog))
18.     for t in range(T):
19.         for i in EverythingInExistence:
20.             i.update()
21. class Universe:                                                                        # a generic world
22.
23.     def __init__(self, x, y, prog=[], devi=[]):                         # init the universe
24.
25.         self.template =  [[0 for i in range(y)] for j in range(x)]                # this is just here in case i need it
26.
27.         self.univ = [[0 for i in range(y)] for j in range(x)]                         # actually init the the universe
28.
29.         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
30.
31.         if prog and devi:                                                         # prog stands for progenitor, devi for deviation
32.             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
33.         elif prog:
34.             univ = [[prog[i][j] for i in range(x)] for j in range(y)
35.     def getNextState(self,x,y):                                                  # simple implementation of CGoL, with some deviations
36.         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
37.                 return 0
38.         sum = [sum(i) for i in univ[y-1:y+1]                                           # generic sum of neighbors
39.         if sum = 3:
40.             return 1
41.         elif sum = 2 and (univ[x][y] = 1):
42.             return 1
43.         elif sum > 6:                                                                  ## PAY ATTENTION, this where stuff starts to get interesting ##
44.             self.branch(sum - 6)                                                 ### In this case, neighbors greater than 6 cause time travel.
45.                                                                                 ### This is arbitrary and not integral to the general system
46.             return 0                                                                  # 0 is returned because the branch creates a universe where 1 was returned in it's stead.
47.          else:
48.             return 0
49.     def update(self,univ):                                                        # global update
50.         temp = univ                                                                  # in anticipation of a branch
51.
52.         for i in range(y):                                                        # decrements the times on the buffer, unless they aren't carrying a value.
53.             for i in range(x):
54.                 if not timeBuffer[x][y][0] == None:
55.                     timeBuffer[x][y] = [timeBuffer[x][y][0], max(0, timeBuffer[x][y][1] -1)]
56.
57.         self.univ = [[self.getNextState(i,j) for i in range(self.y)] for j in range(self.x)]         # updates every cell
58.         if self.branches:                                                        # list containing cells based on whether or not the branch function was called
59. when updating cells
60.             theRealBranch(self.timeBuffer)
61.         for i in range(y):                                                        # removes inconsistent timelines
62.             for j in range(x):
63.                 if timeBuffer[x][y][1] == 0 and not timeBuffer[x][y][0] == univ[x][y]:
64.                     EverythingInExistence.remove(self)                                # removes self
65.         branches = []
66.     def branch(self, self.sum):                                                # doesn't actually branch, just lets theRealBranch function do it's job
67.         self.branches.append([x,y,sum - 6])
68.         self.timeBuffer[x][y] = (0, sum - 6)
69.     def theRealBranch(self, self.timeBuffer, self.branches):                        ### the Magnum Opus of this program, branches the universes
70.         creating = True
71.         while creating:
72.             counter = 0
73.             list = [0 for i in branches]                                        # this list will iterate over all 2^len(branches) universes
74.             for i in list:
75.                 tempTimeBuffer = self.timeBuffer                                # There are just temporary variables here
76.                 tempDevi = self.template                                        # while their universe is being created
77.
78.                 counter += 1                                                        # keep track of where we are
79.
80.                 tempDevi[branches[counter][x]][branches[counter][1]] = list[counter] # this looks scary, bu it just take coordinates from branches and assigns
81. them values based off list
82.                 for i in branches:                                                # updates timeBuffer to prevent weird stuff (tm) from happening in the new
83. universe
84.                     tempTimeBuffer[x][y] = [list[counter], branches[2]]                #
85.                 EverythingInExistence.append(Universe( self.x, self.y, self.univ, tempDevi)  # indented to this to allow multiple creations
86.                 myCreation = EverythingInExistence(len(EverythingInExistence)-1)
87.                 myCreation.timeBuffer = tempTimeBuffer
88.             list[counter] += 1
89.             if list[counter] = 2:
90.                 for i in range(list):                                                # list is represented as a binary number, and incremented
91.                     j = len(list) - i
92.                     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
93. as a binary
94.                         list[j] -= 1
95.                         list[j-1]+=1
96.                     elif j = 0 and list[j] = 2:                                        # j has exceded it's max value
97.                         creating = False                                        # Let there be light
98.                         break
99.                     else:
100.                         break
101.             else:
102.                 pass
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top