Last active
August 29, 2015 14:10
-
-
Save kensaggy/271a9467b4103b6bc56a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
def get_x_y(i,j, size): | |
""" | |
Returns the x,y coordinates in the "big" grid | |
""" | |
return (i/size, j/size) | |
def corner(i,j,size): | |
""" | |
Returns whether a (i,j) coordinate is a corner of a inner box | |
It does this by taking the i,j cooardinates and permutes them with +1 and -1 values for each | |
Then tests if any of those permutations fall on the "border", (the modulo) | |
""" | |
return True if filter((lambda x: (x[0]%size)+(x[1]%size) ==0), [(i+a,j+b) for a in [-1,1] for b in [-1,1]]) else False | |
def get_symbol(i,j,boxes, size, spots): | |
""" | |
Returns the proper symbol that should be displayed at coordinates (i,j) considering (boxes*boxes) matrix, such that every inner square is of size 'size'*'size' | |
and filling in the coordinates specified by the set of tuples, spots (those are given in x,y coordinates | |
""" | |
if j % size == 0 or i%size ==0: | |
return '+' | |
else: | |
return '@' if get_x_y(i,j,size) in spots and not corner(i,j,size)else ' ' | |
def glider(boxes, size, spots): | |
size = size + 1 | |
for i in range(size*boxes+1): | |
for j in range(size * boxes+1): | |
print get_symbol(i,j,boxes,size,spots), | |
def microglider(b,s,sp): | |
s = s+1 | |
r = range(s*b+1) | |
o = map(lambda x: (lambda i,j,b,sp: '+' if j%s == 0 or i%s==0 else '@' if (i/s, j/s) in sp and not (True if filter((lambda x: (x[0]%s)+(x[1]%s) ==0), [(i+n,j+m) for m in [-1,1] for n in [-1,1]]) else False) else ' ')(x[0],x[1],b,sp), [(i,j) for i in r for j in r]) | |
for i,_ in enumerate(o): | |
if i%((b*s)+1) == 0: | |
print _, | |
if __name__ == "__main__": | |
positions = {(0,1), (2,0), (2,1), (2,2)} | |
glider(3, 5, positions) | |
print "Compared to:" | |
microglider(3, 5, positions) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment