Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. import itertools
  2. import time
  3.  
  4. true = True
  5. false= False
  6.  
  7. def bf(width,height,g):
  8. row = [(0,1)] * width
  9. col = (itertools.product(*row) for _ in range(height))
  10. a = machine(list(itertools.product(*col)),g)
  11. return a
  12.  
  13. def machine(x,g):
  14. j=0
  15. for i,k in enumerate(x):
  16. if tran(x[i][0][0],x[i][0][1],x[i][1][0],x[i][1][1]) == g[0][0]:
  17. if tran(x[i][-1][0],x[i][-1][1],x[i][-2][0],x[i][-2][1]) == g[-1][0]:
  18. if tran(x[i][0][-2],x[i][1][-2],x[i][1][-1],x[i][0][-1]) == g[0][-1]:
  19. if tran(x[i][-2][-1],x[i][-2][-2],x[i][-1][-2],x[i][-1][-1]) == g[-1][-1]:
  20. if tran(x[i][1][1],x[i][1][2],x[i][2][1],x[i][2][2]) == g[1][1]:
  21. if nk(k) == g:
  22. j += 1
  23. return j
  24.  
  25. def tran(a,b,c,d):
  26. if a+b+c+d == 1:
  27. return 1
  28. else:
  29. return 0
  30.  
  31. def nk(arr):
  32. width = len(arr[0])
  33. height = len(arr)
  34. arr_new = [[0 for x in range(0)] for y in range(height-1)]
  35. for x in xrange(0,height-1):
  36. for y in xrange(0,width-1):
  37. if arr[x][y] + arr[x][y+1] + arr[x+1][y] + arr[x+1][y+1] == 1:
  38. arr_new[x].append(1)
  39.  
  40. else:
  41. arr_new[x].append(0)
  42. return arr_new
  43.  
  44. def answer(g):
  45. start = time.time()
  46. width = len(g[0]) + 1
  47. height = len(g) + 1
  48. print bf(width,height,g)
  49. print "Time : ", (time.time()-start)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement