public
Created

  • Download Gist
gen_tiling.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
import sys
import copy
 
def gen_emp_tree(num):
l = []
for i in range(4):
l.append(['']*num)
return l
 
 
def is_not_good(l):
n = len(l[0])
 
for i in range(4):
for j in range(n):
if j < n-1 and l[i][j] == '' and l[i][j+1] == '':
return False
 
if i < 3 and l[i][j] == '' and l[i+1][j] == '':
return False
 
return True
 
 
def add_horz_2(l):
n = len(l[0])
 
for i in range(4):
for j in range(n-1):
if l[i][j] == '' and l[i][j+1] == '':
l[i][j] = l[i][j+1] = 'h'
return l
 
return None
 
def add_vert_2(l):
n = len(l[0])
 
for i in range(3):
for j in range(n):
if l[i][j] == '' and l[i+1][j] == '':
l[i][j] = l[i+1][j] = 'v'
return l
 
return None
 
 
def add_2by2(l):
n = len(l[0])
 
for i in range(3):
for j in range(n-1):
if l[i][j] == '' and l[i][j+1] == '' and l[i+1][j] == '' and l[i+1][j+1] == '':
l[i][j] = l[i][j+1] = l[i+1][j] = l[i+1][j+1] = 'H'
return l
 
return None
 
 
def gen_trees(l):
if is_not_good(l):
return l
 
n = []
 
horz2 = add_horz_2(copy.deepcopy(l))
if horz2:
n += gen_trees(horz2)
 
vert2 = add_vert_2(copy.deepcopy(l))
if vert2:
n += gen_trees(vert2)
 
horz2by2 = add_2by2(copy.deepcopy(l))
if horz2by2:
n += gen_trees(horz2by2)
 
return n
 
def main():
if len(sys.argv) != 2:
print 'give me one num'
sys.exit(1)
else:
num = int(sys.argv[1])
 
l = gen_emp_tree(num)
l = gen_trees(l)
 
lst = []
for i in range(len(l)):
if not i % 4:
check = True
tmp = l[i] + l[i+1] + l[i+2] + l[i+3]
try:
tmp.index('')
check = False
except:
pass
 
if check:
tmp = [l[i], l[i+1], l[i+2], l[i+3]]
try:
lst.index(tmp)
except:
lst.append(tmp)
 
# Print all possible combinations
for i in lst:
for j in i:
print j
print ''
 
print len(lst)
 
if __name__ == '__main__':
main()

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.