Skip to content

Instantly share code, notes, and snippets.

@apaap
Created October 10, 2019 08:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apaap/1a6745ee038540d880a9e7375b18d7dc to your computer and use it in GitHub Desktop.
Save apaap/1a6745ee038540d880a9e7375b18d7dc to your computer and use it in GitHub Desktop.
# Python script for Golly to count active cells within the envelope of a moving pattern
#count_active.py
# Count active cells within the envelope of a moving pattern
import golly as g
import numpy as np
r = g.getrect()
if not r:
g.exit('No pattern')
patt = g.getcells(r)
if len(patt)%2:
g.exit('Only 2-state patterns supported')
# Check every second generation for roomba bugs
g.setbase(2)
g.setstep(1)
# keep track of cells within envelope which have been On or Off
sz = (104, 104) # large enough to fit the pattern when centred
everOn = np.zeros(sz, dtype=bool) # Cells which have been On in any generation
allOn = np.ones(sz, dtype=bool) # Cells which have been On in every generation
allOff = np.ones(sz, dtype=bool) # Cells which have been Off in every generation
active_count = []
# loop over successive even generations
for ii in xrange(1, 50001):
r = g.getrect()
if not r:
g.exit('Pattern has become empty')
patt = g.getcells(r)
ncells = len(patt)/2
# Centre pattern using centre of mass
xmean = int(round(float(sum(patt[0::2])) / ncells))
ymean = int(round(float(sum(patt[1::2])) / ncells))
patt = g.transform(patt, 52-xmean, 52-ymean)
npPatt = np.zeros(sz, dtype=bool)
for (x,y) in zip(patt[0::2], patt[1::2]):
npPatt[x, y] = True
everOn |= npPatt
allOn &= npPatt
# allOff &= ~npPatt # equivalent to ~everOn
g.step()
if not (ii % 1000):
g.update()
active_count.append(everOn.sum() - allOn.sum())
g.show(str(active_count[-20:]))
# count_everOn = everOn.sum()
# count_allOn = allOn.sum()
# count_allOff = allOff.sum()
# g.show(str([count_everOn, count_allOn, count_everOn-count_allOn]))
g.note(str(active_count))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment