def get_patterns(cells, period): if not cells: return # calculate where the gliders first hit the pattern min_lane = 99999 for i in range(0, len(cells), 2): if LANE1 - 3 <= cells[i] + cells[i + 1] <= LANE2 + 6: min_lane = min(min_lane, cells[i] - cells[i + 1]) if min_lane == 99999: return minx = min_lane // 2 g1 = g.transform(G1, minx, -minx) g2 = g.transform(G2, minx, -minx) # Singletons for t in range(period): yield cells + g.evolve(g1, t), t, None # Pairs for phase in range(period): tg2 = g.transform(g.evolve(g2, 2), -4, 4) # -14 gens for t in range(15, MAX_DIFF + 1): tg2 = g.transform(g.evolve(tg2, 3), -1, 1) yield cells + g1 + tg2, phase, phase - t g1 = g.evolve(g1, 1) g2 = g.evolve(g2, 1)
def show_it(recipe, lane, move, elbow_type, start_elbow): global offset start_cells, start_type, start_lane = start_elbow res = "" phase = 0 if lane is not None: direction = lane[1] phase = lane[2] if direction == 0: res = "Rev%d" % lane[0] elif direction == 1: res = "R%d" % (lane[0] - start_lane) elif direction == 2: res = "L%d" % (lane[0] - start_lane) # elif direction == 3: # res = "LWSS_W" # elif direction == 4: # res = "LWSS_S" if move is None: res += "k" else: res += "m%d%s%s" % (move - start_lane, str(start_type), elbow_type) g.putcells(make_text(res, "mono"), offset, -80) g.putcells(start_cells, offset, 0) for i, t in enumerate(recipe[::2]): if t is not None: d = 80*i + MAX_DIFF / 4 + 20 g.putcells(g.evolve(G1, t + MAX_DIFF), offset-d, d) for i, t in enumerate(recipe[1::2]): if t is not None: d = 80*i + MAX_DIFF / 4 + 20 g.putcells(g.evolve(G2, t + MAX_DIFF), offset-d, d) res += ": " for i in range(0, len(recipe), 2): if recipe[i] is None: res += "eo"[(recipe[i+1]+phase)%2] + "-9999 " elif recipe[i+1] is None: res += "eo"[(recipe[i]+phase)%2] + "9999 " else: res += "eo"[(recipe[i]+phase)%2] + str(recipe[i]-recipe[i+1]) + " " f.write(res + "\n") f.flush() offset += 100 g.update()
def FindPeriod(obj): evolved = g.evolve(obj, 1) for i in xrange(1, 1000): if str(evolved) == str(obj): return i evolved = g.evolve(evolved, 1) return -1
def SaveWss(file): g.setrule("b3/s23") wss = [g.parse("bobo$4bo$o3bo$o3bo$4bo$bo2bo$2b3o!"), g.parse("bobo$4bo$o3bo$4bo$bo2bo$2b3o!"), g.parse("obo$3bo$3bo$o2bo$b3o!")] wssList = [] for w in wss: for i in xrange(0, 4): wssList.append(PrepareList(g.evolve(w, i))) wssList.append(PrepareList(g.transform(g.evolve(w, i), 0, 0, 1, 0, 0, -1))) pickle.dump(wssList, open(path.join(g.getdir("data"),file), "wb"))
def __init__(self): self.signalsFullData = [] self.signals = [] self.components = [g.parse("bo$2bo$3o!", -1, -1)] for idx in xrange(0, len(self.components)): comp = self.components[idx] for i in xrange(-1, 2, 2): for j in xrange(-1, 2, 2): for k in xrange(0, 4): self.signalsFullData.append((g.transform(g.evolve(comp, k), 0, 0, i, 0, 0, j), i, j, k, idx)) self.signals.append(g.transform(g.evolve(comp, k), 0, 0, i, 0, 0, j))
def UpdateMove(d, w, h, x0, y0, p, t): under = d[0] obj = d[1] x = d[2] y = d[3] if under != -1: ClearRect(x - w, y - h, 2 * w + 1, 2 * h + 1) g.putcells(under) val = g.getxy() if val == "": return x1 = int(val.split()[0]) y1 = y0 + GetDirection(t) * (x1 - x0) d[0] = g.getcells([x1 - w, y1 - h, 2 * w + 1, 2 * h + 1]) #ClearRect(x1 - w, y1 - h, 2 * w + 1, 2 * h + 1) g.putcells(g.evolve(obj, p + GetEvolveDirection(t) * ((4 * (x1 - x0)) % p)), x1, y1) g.update() d[2] = x1 d[3] = y1
def GunArea(cells, curGunPeriod): maxBox = [] minpop = -100000 for i in xrange(0, curGunPeriod, 4): g.new(str(i)) g.putcells(g.evolve(cells, i)) g.setbase(8) g.setstep(3) g.step() g.step() edgeGlider = EdgeGlider() while PerformDelete(edgeGlider, curGunPeriod): edgeGlider = DevolveGlider(edgeGlider, curGunPeriod) for j in xrange(0, 4): if g.getpop() > minpop: maxpop = g.getpop() maxpopgun = g.getcells(g.getrect()) maxBox = AppendBox(maxBox, g.getrect()) g.run(1) return [BoxValue(maxBox), maxpopgun, maxBox]
def GunArea(cells, curGunPeriod): maxBox = [10000, 10000, -1000, -1000] for i in xrange(0, curGunPeriod, 4): g.new(str(i)) g.putcells(g.evolve(cells, i)) g.setbase(8) g.setstep(3) g.step() g.step() g.step() g.step() edgeGlider = EdgeGlider() while PerformDelete(edgeGlider, curGunPeriod): edgeGlider = DevolveGlider(edgeGlider, curGunPeriod) for j in xrange(0, 4): maxBox = AppendBox(maxBox, g.getrect()) g.run(1) if i == 0: somegun = g.getcells(g.getrect()) return [BoxValue(maxBox), somegun, maxBox]
def reconstruct(gstr, stepback=2): """Reconstruct a pattern representing a glider set from its (canonical) string. The transformation is assumed to be the identity. Returns a single Golly cell list with all gliders at <stepback> gen prior to canonical time. """ fields, at, trans_str = gstr.partition("@") res = [] glider = g.parse("bo$2bo$3o") # SE # Process transformation # XXX unimplemented (but not required here) t, o, shift_x, shift_y = 0, "identity", 0, 0 # Step back to separate gliders (same as Shinjuku uses for realising syntheses) t += stepback # Process glider sets for i, field in enumerate(gstr.split("/")): salvo = [] for (time, lane) in zip(*[iter(field.split())] * 2): time, lane = - int(time) - t - 4, int(lane) dist, time = time // 4, time % 4 salvo += g.evolve(g.transform(glider, dist, dist - lane), time) if i == 1: salvo = g.transform(salvo, 0, 0, r270[0], r270[1], r270[2], r270[3]) # "rot270" elif i == 2: salvo = g.transform(salvo, 0, 0, r180[0], r180[1], r180[2], r180[3]) # "rot180" elif i == 3: salvo = g.transform(salvo, 0, 0, r90[0], r90[1], r90[2], r90[3]) # "rot90" res += salvo return g.transform(res, shift_x, shift_y)
def __init__(self): self.signalsFullData = [] self.signals = [] self.components = [g.parse("bo$2bo$3o!", -1, -1)] for idx in xrange(0, len(self.components)): comp = self.components[idx] for i in xrange(-1, 2, 2): for j in xrange(-1, 2, 2): for k in xrange(0, 4): self.signalsFullData.append( (g.transform(g.evolve(comp, k), 0, 0, i, 0, 0, j), i, j, k, idx)) self.signals.append( g.transform(g.evolve(comp, k), 0, 0, i, 0, 0, j))
def is_there_glider(x, y): for i in range(4): gl = g.evolve(gld, i) if is_there(x, y, gl): return i return -1
def SaveWss(file): g.setrule("b3/s23") wss = [ g.parse("bobo$4bo$o3bo$o3bo$4bo$bo2bo$2b3o!"), g.parse("bobo$4bo$o3bo$4bo$bo2bo$2b3o!"), g.parse("obo$3bo$3bo$o2bo$b3o!") ] wssList = [] for w in wss: for i in xrange(0, 4): wssList.append(PrepareList(g.evolve(w, i))) wssList.append( PrepareList(g.transform(g.evolve(w, i), 0, 0, 1, 0, 0, -1))) pickle.dump(wssList, open(path.join(g.getdir("data"), file), "wb"))
def run_pattern_in_golly(pattern, comments, extended): if extended: try: extended = int(pattern[pattern.index('%')+1:]) except ValueError: #sometimes there's a % sign in the comments extended = False pattern_rle = pattern pattern = g.parse(pattern) if len(pattern) % 2 == 1: #multistate rule for some reason g.warn(pattern_rle) g.warn(str(pattern)) initial_pattern = pattern.copy() xs = pattern[::2] ys = pattern[1::2] min_x = 0 max_x = max(xs) min_y = 0 max_y = max(ys) min_min_x = min_x #these four are the permanent minima and maxima, used for determining maximum pattern size max_max_x = max_x min_min_y = min_y max_max_y = max_y for period in range(1, 1000): #maximum oscillator period if period == 999 and extended: pattern = g.evolve(pattern, extended - 999) pattern = g.evolve(pattern,1) if not pattern: g.warn('Not an oscillator, dies out completely: %s' % initial_pattern) return xs = pattern[::2] ys = pattern[1::2] min_min_x = min(min_min_x, min(xs)) #sets new absolute minima and maxima max_max_x = max(max_max_x, max(xs)) min_min_y = min(min_min_y, min(ys)) max_max_y = max(max_max_y, max(ys)) if pattern == initial_pattern: if extended: return (comments + convert_grid_to_rle(pattern), extended, max_max_x-min_min_x+1, max_max_y-min_min_y+1, -min_min_x, -min_min_y) else: return (comments + convert_grid_to_rle(pattern), period, max_max_x-min_min_x+1, max_max_y-min_min_y+1, -min_min_x, -min_min_y) #0: RLE. 1: period. 2, 3: maximum bounding box for x and y. 4, 5: Greatest negative for calculating offset. #if extended == 'file': #one at a time # return pattern g.warn('Not an oscillator, maximum generations reached: %s' % initial_pattern) return
def MoveToX(self, attachedPat, x1, movementData): if (x1 - movementData.initX) * GetPositive(attachedPat.logicPat.t) < 0: x1 = movementData.initX y1 = movementData.initY + GetDirection(attachedPat.logicPat.t) * (x1 - movementData.initX) movementData.RevertState() movementData.UpdateState(g.evolve(movementData.initPat, 4 * (x1 - movementData.initX) * GetPositive(attachedPat.logicPat.t)), x1, y1)
def delay_construction(obj_list, delay): pat = [] phase = -delay % 4 n = (delay + phase) // 4 for cells, dx, dy in obj_list: pat += g.transform(g.evolve(cells, phase), -n * dx, -n * dy) return pat
def find_all_glider_idx(mask): idxs = extract_indexes(mask) idxs.sort(key=lambda idx: (1.01 * gliders_in[idx][0] + gliders_in[idx][1])) copy_idxs = idxs[:] for edge_i in enum_shooters(mask): reaction_cells = shoot_defs[edge_i][4] stable_cells = shoot_defs[edge_i][5] for g_i in list(copy_idxs): g.new("") for g_j in idxs: x, y, idx = gliders_in[g_j] if g_j == g_i: g.putcells(g.evolve(reaction_cells, idx), x, y) else: g.putcells(g.evolve(gld, idx), x - 128, y + 128) g.setbase(8) g.setstep(3) g.step() x, y, _ = gliders_in[g_i] # test if the pattern consists of the stable cells plus the # necessary gliders and nothing else g.putcells(stable_cells, x, y, 1, 0, 0, 1, "xor") if int(g.getpop()) != 5 * len(idxs): continue for g_j in idxs: x, y, idx = gliders_in[g_j] g.putcells(g.evolve(gld, idx), x, y, 1, 0, 0, 1, "xor") if g.empty(): copy_idxs.remove(g_i) yield g_i,edge_i
def test(pat, gen, loc, x, y): global results if not all(g.getcell(x + loc, y) for x, y in on_cells): return if any(g.getcell(x + loc, y) for x, y in off_cells): return begin = start_cells + g.evolve(g.transform(pat, -x, -y), gen) if finalpop >= 0 and len(g.evolve(begin, 100)) != 2 * finalpop: return results += 1 g.setlayer(results_layer) g.putcells(g.evolve(pat, gen % 8), 50 * results - x, -y) g.putcells(begin, 50 * results, 50) g.putcells(g.evolve(begin, delay), 50 * results, 100) g.setlayer(work_layer)
def MoveToX(self, attachedPat, x1, movementData): if (x1 - movementData.initX) * GetPositive(attachedPat.logicPat.t) < 0: x1 = movementData.initX y1 = movementData.initY + GetDirection( attachedPat.logicPat.t) * (x1 - movementData.initX) movementData.RevertState() movementData.UpdateState( g.evolve( movementData.initPat, 4 * (x1 - movementData.initX) * GetPositive(attachedPat.logicPat.t)), x1, y1)
def FinishMove(d, w, h, x0, y0, p, t): under = d[0] obj = d[1] x = d[2] y = d[3] if under != -1: g.putcells(under) g.putcells(g.evolve(obj, p + GetEvolveDirection(t) * ((4 * (x - x0)) % p)), x, y) g.update()
def evolve(self, N): """\ Return N-th generation of the pattern. Once computed, the N-th generation is remembered and quickly accessible. It is also the base for computing generations subsequent to N-th.""" if N < 0: raise ValueError("backward evolving requested") if self.__phases.has_key(N): return self.__phases[N] M = 0 for k in self.__phases.keys(): if M < k < N: M = k p = self.__phases[N] = pattern(golly.evolve(self.__phases[M], N - M)) return p
def find_best_selection(): r = g.getrect() all = g.getcells(r) sep = 1 # - run the pattern for 4096 ticks, get the new settled pattern # - try XORing new pattern with original pattern for every possible offset up to 512 # - one of the offsets should give the lowest total population # (will probably decrease the population instead of increasing it, # unless what is being built is a prolific puffer or gun or some such) bestscore, bestsep = len(all), -1 # = population * 2 allplus4096 = g.evolve(all, 4096) g.addlayer() while sep <= 512: g.show("Finding stage spacing -- testing " + str(sep)) g.new("sep=" + str(sep)) g.putcells(all) g.putcells(allplus4096, sep, 0, 1, 0, 0, 1, "xor") score = int(g.getpop()) if bestscore > score: bestscore, bestsep = score, sep sep += 1 g.dellayer() sep = bestsep g.show("found separation: " + str(sep)) bestblockscore, bestoffset = -999999, -1 for offset in range(sep): g.select([r[0] - offset, r[1], sep, r[3]]) g.update() blockscore = 0 for blockx in range(r[0] - offset, r[0] + r[2], sep): g.select([blockx, r[1], sep, r[3]]) blockrect = g.getselrect() block = g.getcells(blockrect) if len( block ) == 0: # ran into empty block, this must not be the right separation g.exit("Invalid pattern format found at separation = " + str(sep) + ": selected block is empty.") g.shrink(1) shrunkblockrect = g.getselrect() leftdiff = shrunkblockrect[0] - blockrect[0] rightdiff = (blockrect[0] + blockrect[2]) - (shrunkblockrect[0] + shrunkblockrect[2]) blockscore += leftdiff + rightdiff if leftdiff < 10: blockscore -= (10 - leftdiff)**2 if rightdiff < 10: blockscore -= (10 - rightdiff)**2 if blockscore > bestblockscore: bestblockscore, bestoffset = blockscore, offset g.select([r[0] - bestoffset, r[1], r[2] + offset, r[3]]) return sep
def GetObjectArray(iniobj): result = [] transList = GetTransList() period = FindPeriod(iniobj[0]) for i in xrange(0, period): obj = g.evolve(iniobj[0], i) for t in transList: dxx, dxy, dyx, dyy = t curobj = g.transform(obj, 0, 0, dxx, dxy, dyx, dyy) result.extend(GetObjectClickArray(curobj, iniobj[1], t, period)) return result
def Add(self, logicPat): for t in self.transList: dxx, dxy, dyx, dyy = t cells = g.transform(logicPat.cells, 0, 0, dxx, dxy, dyx, dyy) if dxx == 0: cells = g.transform(g.evolve(cells, 2), -dxy, 0) inT = TrnasformDirectionList(logicPat.inputs, t) outT = TrnasformDirectionList(logicPat.outputs, t) p = logicPat.period pat = NewLogicalPattern(cells, inT, outT, p, t) self.patterns.append(pat)
def Add(self, logicPat): for t in self.transList: dxx, dxy, dyx, dyy = t cells = g.transform(logicPat.cells,0, 0, dxx, dxy, dyx, dyy) if dxx == 0: cells = g.transform(g.evolve(cells, 2), -dxy, 0) inT = TrnasformDirectionList(logicPat.inputs, t) outT = TrnasformDirectionList(logicPat.outputs, t) p = logicPat.period pat = NewLogicalPattern(cells, inT, outT, p, t) self.patterns.append(pat)
def getallorientations(clist, maxticks, matchtype=0): uniques = [] xforms = [] uhashes = [] orients = [] dticks = [] ticks = 0 rangemax = (8 if matchtype == 0 else matchtype ) # 1 (no rot/ref) or 4 (rotation only) are reasonable nomatch = 1 while nomatch: # check next generation until a repeat occurs in orientation 1 for i in range( rangemax ): # TODO: invent a structure to avoid having to use indexes like this pat, xform = getorientation(clist, i) h = gethash(pat) if uhashes.count(h) == 0: uniques.append(pat) uhashes.append(h) xforms.append(xform) orients.append(i) dticks.append(ticks) else: if i == 0: nomatch = 0 # if we've already seen the first phase, we've seen them all from here on out. break # (for other phases the duplication might be due to symmetry, so we try them all) if nomatch == 0: break # TODO: this is silly -- find a better structural way to do this ticks += 1 if maxticks <= 0: if ticks >= 512: g.note( "No periodicity found within 512 ticks -- ending search.") nomatch = 0 break else: g.note( str(ticks) + " :: " + str(maxticks) + " :: " + str(ticks >= 1)) ##################### if ticks >= maxticks: nomatch = 0 break # stop collecting patterns silently if a maximum has been specified clist = g.evolve(clist, 1) x, y = findTLsorted(clist) return [uniques, xforms, orients, dticks]
def get_invariants(cells): invariants = [] for t in range(0, 4): gliders = [] temp_cells = cells[:] for gen in range(0, 4): gliders.extend([coords + (gen,) for coords in find_all_gliders(temp_cells)]) temp_cells = g.evolve(temp_cells, 1) glider_pairs = combinations(gliders, 2) invariants.extend([item for pair in glider_pairs for item in compute_invariants(pair)]) cells = g.transform(cells, 0, 0, 0, -1, 1, 0) seen = set() return [ item for item in invariants if item not in seen and not seen.add(item) ]
def get_invariants(cells): invariants = [] for t in range(0, 4): gliders = [] temp_cells = cells[:] for gen in range(0, 4): gliders.extend( [coords + (gen, ) for coords in find_all_gliders(temp_cells)]) temp_cells = g.evolve(temp_cells, 1) glider_pairs = combinations(gliders, 2) invariants.extend([ item for pair in glider_pairs for item in compute_invariants(pair) ]) cells = g.transform(cells, 0, 0, 0, -1, 1, 0) seen = set() return [ item for item in invariants if item not in seen and not seen.add(item) ]
def store(cells, lane, recipe, period, depth, next_pats): old_depth = -1 # ignore parity of output glider when canonicalising. # assumes we can change parity by delaying by one tick. lane_str = "_None" if lane is None else "_%d_%d" % lane[:2] canon = canonical(cells) + lane_str if canon in depths: old_depth = depths[canon] elif period == 2: canon1 = canonical(g.evolve(cells, 1)) + lane_str if canon1 in depths: old_depth = depths[canon1] if old_depth < depth: depths[canon] = depth if depth > 0: next_pats.append((cells, lane, recipe, period, depth)) return True else: return False
MAX_DIFF = 60 OUTFILE = '/home/user/life/outfile%d.txt' % time() def to_pairs(cells): return zip(cells[::2], cells[1::2]) G_NE = g.parse('3o$2bo$bo!') G_NW = g.parse('3o$o$bo!') G_SW = g.transform(g.parse('bo$o$3o!'), 0, -2) G_SE = g.transform(g.parse('bo$2bo$3o!'), -2, -2) LWSS_W = g.transform(g.parse('bo2bo$o$o3bo$4o!'), 0, -1) LWSS_S = g.transform(g.parse('bobo$o$o$o2bo$3o!'), -2, -4) GLIDERS_SW = [to_pairs(g.evolve(G_SW, i)) for i in range(4)] GLIDERS_SE = [to_pairs(g.evolve(G_SE, i)) for i in range(4)] GLIDERS_NW = [to_pairs(g.evolve(G_NW, i)) for i in range(4)] LWSSES_W = [to_pairs(g.evolve(LWSS_W, i)) for i in range(4)] LWSSES_S = [to_pairs(g.evolve(LWSS_S, i)) for i in range(4)] assert(all((0,0) in gl for gl in GLIDERS_SW)) assert(all((0,0) in gl for gl in GLIDERS_SE)) assert(all((0,0) in gl for gl in GLIDERS_NW)) assert(all((0,0) in lwss for lwss in LWSSES_W)) assert(all((0,0) in lwss for lwss in LWSSES_S)) def get_g0(lane): x = lane // 2 - 5 glider = g.transform(G_NE, x, lane - x) return g.evolve(glider, 2 * (1 + lane % 2))
def post_state(current_state, obj_list, delay): new_state = delay_construction(obj_list, delay) return g.evolve(current_state + new_state, delay + 256)
def Evolve(self, numIters): self.cells = g.evolve(self.cells, numIters)
outfile = open(filename + '.out', 'w') for line in infile: pattern, _, _, pattern_type = re.split('\s', line, 5)[:4] if pattern_type != 'other': continue pattern = re.sub('!', '.\n', pattern) cells = g.parse(pattern) gliders = [] temp_cells = cells[:] for gen in range(0, 4): gliders.extend(find_all_gliders(temp_cells)) temp_cells = g.evolve(temp_cells, 1) if len(gliders) < 2: continue sample = get_glider_color(gliders[0]) if not all([get_glider_color(glider) == sample for glider in gliders[1:]]): continue # check that pattern emits exactly one orthogonal glider new_cells = g.evolve(cells, 700) emitted_gliders = [] for t in range(0, 4): emitted_gliders.append(0) temp_cells = new_cells
def test(cells, lane): cells2 = g.evolve(cells, 4) if len(cells) != len(cells2): return 0, [], None sumx1, sumy1 = sum(cells[::2]), sum(cells[1::2]) sumx2, sumy2 = sum(cells2[::2]), sum(cells2[1::2]) delta = (sumx2 - sumx1, sumy2 - sumy1) for _ in range(4): cells2 = g.evolve(cells2, 4) sumx1, sumy1 = sumx2, sumy2 sumx2, sumy2 = sum(cells2[::2]), sum(cells2[1::2]) new_delta = (sumx2 - sumx1, sumy2 - sumy1) if new_delta != delta: return 0, [], None # a,b,c,d,e are used to convert x, y values into a lane number and output type if delta == (0, 0): spaceships = [] elif delta == (-5, 5): spaceships = GLIDERS_SW a, b, c, d, e = 1, 1, -1, 0, 0 elif delta == (5, 5): spaceships = GLIDERS_SE a, b, c, d, e = 1, -1, 1, 0, 1 elif delta == (-5, -5): spaceships = GLIDERS_NW a, b, c, d, e = 1, -1, 2, 0, 2 # elif delta == (-18, 0) or delta == (-24, 0): # spaceships = LWSSES_W # a, b, c, d, e = 0, 2, 0, 1, 3 # elif delta == (0, 18) or delta == (0, 24): # spaceships = LWSSES_S # a, b, c, d, e = 2, 0, 0, 1, 4 else: return 0, [], None pairs = to_pairs(cells) if spaceships and lane is None: found = False for x0, y0 in pairs: for phase, ss in enumerate(spaceships): if all((x0+i, y0+j) in pairs for (i, j) in ss): for i, j in ss: pairs.remove((x0+i, y0+j)) found = True lane = a * x0 + b * y0 + c + d * (x0+y0)%2, e, phase % 2 break if found: break if not found: return 0, [], None cells = [] for x, y in pairs: cells.append(x) cells.append(y) sort = sorted(pairs) for p in range(2): cells = g.evolve(cells, 1) if sorted(to_pairs(cells)) == sort: return p + 1, cells, lane return 0, [], None
def search(elbow): global depths start_elbow = elbow, is_elbow(elbow), max_lane(elbow) #assume elbow is p1 new_pats = [(elbow, None, (), 1, FULL_DEPTH)] depths = {} start = True iteration = 0 while new_pats: iteration += 1 next_pats = [] n = 0 for cells, lane, recipe, period, depth in new_pats: g.show(str((start_elbow[1], iteration, n, len(new_pats)))) n += 1 # only fire stuff at an elbow at the very beginning if not start and is_elbow(cells): continue start = False for start_cells, t1, t2 in get_patterns(cells, period): end_cells = g.evolve(start_cells, GENS) if len(end_cells) > 2 * (MAX_POP[iteration-1] + 12): continue new_period, end_cells, new_lane = test(end_cells, lane) if new_period == 0: continue if len(end_cells) > 2 * MAX_POP[iteration-1]: continue new_depth = depth - 1 new_recipe = recipe + (t1, t2) if lane is None and new_lane is not None: new_depth += CLEANUP_DEPTH if store(end_cells, new_lane, new_recipe, new_period, new_depth, next_pats): # Elbow killing recipes # if new_lane is not None and not end_cells: # show_it(new_recipe, new_lane, None, startelbow) elbow = is_elbow(end_cells) if elbow: move = max_lane(end_cells) show_it(new_recipe, new_lane, move, elbow, start_elbow) new_pats = next_pats
def cellsort(clist): return g.evolve(clist, 0)
def f(cells): return g.evolve(g.transform(cells, 1, 2, 0, -1, -1, 0), 2)
] if g.numstates() != 2: g.exit( "Please convert to standard two-state Life before running this script." ) clist = g.getcells(g.getselrect()) if len(clist) != 20: g.exit("Please select a rectangle containing the two gliders to be synchronized, and nothing else." \ + "\nThere should be at least a four-cell gap between the gliders horizontally and vertically.") while 1: # iterate until we find NW0 stats1, stats2 = findgliders(clist) canonical1 = findcanonical(stats1, "NW") canonical2 = findcanonical(stats2, "SE") if canonical1[1] == 0: break clist = g.evolve(clist, 1) dir1, phase1, x1, y1 = canonical1 dir2, phase2, x2, y2 = canonical2 charx = "E" if ( x1 - x2) % 2 else "O" # looks backwards -- the usual fencepost issue chary = "e" if ( y1 - y2 ) % 2 else "o" # (bounding box coords vs. # of cells in bounding box width) rating = charx + chary + str(phase2) clist = g.getcells(g.getselrect()) while 1: # iterate until we find NW0 stats1, stats2 = findgliders(clist) stats2mod = stats2[:] stats2mod[2] -= 1024
def DevolveGlider(glider, delta): return g.evolve(g.transform(glider, -delta, -delta), 3 * delta)
gliders_in = find_and_remove_all() init_shooters() prev = a_star_search() step_d = 250 total_d = 0 g.new("") current = 0 start = 2 ** len(gliders_in) - 1 while current != start: i, edge_i = prev[current] current ^= 2 ** i x, y, idx = gliders_in[i] _, edge_shoot, left_g, right_g, _, _ = shoot_defs[edge_i] g.putcells(edge_shoot, x + total_d, y - total_d,) g.putcells(g.evolve(left_g, idx), x, y - 2 * total_d) g.putcells(g.evolve(right_g, idx), x + 2 * total_d, y) total_d += step_d g.fit() g.update()
def DrawAll(self): for pat in self.patterns: x, y = pat.location g.putcells(g.evolve(pat.attachedPat.logicPat.cells, pat.internal), x, y)
def getbackground(clist): g.setrule("B12345678/S") background = g.evolve(clist, 1) g.setrule("B3/S23") return background
import golly as g glds = [[],[]] glds[0] = g.parse("3o$o$bo!") glds[1] = g.evolve(g.parse("3o$o$bo!"), 2) #blck = g.parse("2o$2o!", -4, 0) def place_glider(i, dx, dy): if i%2 == 0: g.putcells(glds[0], 15 * int(i/2) + dx, 15 * int(i/2) + dy) else: g.putcells(glds[1], 8 + 15 * int(i/2) + dx, 8 + 15 * int(i/2) + dy) class ArmP30: def __init__(self): self.recipe = "" self.move_recipes = {} self.shoot_recipes = {} self.arm_ext = -42 def load_recipes(self): path = path = r'C:\Users\SimSim314\Documents\GitHub\GreyGooTheory\P30Gemini' file = open(path + r'\shoot.recipe.txt', 'r') for line in file: recipe = line.split(":") recipe[1] = int(recipe[1].replace("B", "")) recipe[2] = int(recipe[2].replace("G", ""))
def get_g0(lane): x = lane // 2 - 5 glider = g.transform(G_NE, x, lane - x) return g.evolve(glider, 2 * (1 + lane % 2))
def add_it(objs, s, dx, dy): cells = g.parse(s) for i in range(4): objs.append((make_target(cells), dx, dy)) cells = g.evolve(cells, 1)
def makeRLEline(pat): return giveRLE(list(itertools.chain(*pat))) patdict = {} objlist = [] for i in range(len(objs)): # normalize so that the first ON cell in the list is always (0,0) templist = g.parse(objs[i]) objlist += [g.transform(templist, -templist[0], -templist[1])] numobjs = len(objlist) zonelist = [] for item in objlist: g.setrule("B12345678/S012345678") neighbors = g.evolve(item, 1) g.setrule("B12345678/S012345678" ) ######### this is "B2345678/S012345678" for Conway's Life zone = g.evolve(neighbors, 1) zonelist += [zone] # includes cells for object also g.setrule("LifeHistory") nearlist = [[i, j] for i in range(-1, xsize + 1) for j in range(-1, ysize + 1) if i < 0 or i >= xsize or j < 0 or j >= ysize] count, x, y, ptr, filledlist, searchlist = 0, 0, 0, 0, [], [] while y == 0 or len(searchlist) > 0: overlap = ([x, y] in nearlist) # place current object ############################################# # TODO: if there's an overlap, set ptr to max value, then handle all cases with same code at the bottom if overlap == 0:
} """ def cell_set(cell_list): cells = set() while len(cell_list) > 1: cells.add((cell_list.pop(0),cell_list.pop(0))) return cells # get initial cells initial = golly.getcells(rect) # create cell sets for all generations generations = [cell_set(golly.evolve(initial,c)) for c in range(count)] # dump preamble to output ofile.write(preamble) # union of all cell transitions ofile.write("union() { \n") def buildLayerCubes(bottom,top,z,ofile): for (x,y) in bottom: candidates = set([(xp,yp) for xp in range(x-1,x+2) for yp in range(y-1,y+2)]) for (xp,yp) in candidates.intersection(top): ofile.write("span_cube({0},{1},{2},{3},{4});\n".format(x,y,xp,yp,z)) for i in xrange(count-1): buildLayerCubes(generations[i],generations[i+1],i,ofile)