Skip to content

Instantly share code, notes, and snippets.

@joshourisman
Created October 4, 2012 03:03
Show Gist options
  • Save joshourisman/3831229 to your computer and use it in GitHub Desktop.
Save joshourisman/3831229 to your computer and use it in GitHub Desktop.
import sys
def equal(triplet):
return len(set(triplet)) == 1
def finished(world):
for i in range(len(world)):
if len(world[i:]) < 3:
return True
triplet = world[i:i + 3]
if equal(triplet):
return False
def collapse(world):
print(world)
i = 0
while not finished(world):
triplet = world[i:i + 3]
if equal(triplet):
world.insert(i, sum([world.pop(i), world.pop(i), world.pop(i)]))
print(world)
i = 0
else:
i += 1
if __name__ == '__main__':
num = int(sys.argv[1])
world = [1 for i in range(num)]
collapse(world)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment