fork download
  1. import itertools as I
  2. E=enumerate
  3. R=lambda x,n=4:{str(x),*R([i[::-1]for i in zip(*x)],n-1)}if n else set()
  4. S=lambda t:{(x,y)for x,k in E(t)for y,p in E(k)if p}
  5. G=lambda p,i,F=min:F(j[i]for j in p)
  6. O=lambda j:{*R(m:=[[int((x,y)in j)for y in range(G(j,1,max)+1)]for x in range(G(j,0,max)+1)]),*R([i[::-1]for i in m])}
  7. def P(b,n):
  8. q=[(b,[],n)]
  9. while q:
  10. b,C,n=q.pop(0)
  11. if not(n or b):yield C
  12. if n and b:
  13. for i,_ in E(b,1):
  14. for c in I.combinations(b,i):
  15. if 1==len(c)or all(any((j+J,k+K)in c for J,K in[(0,1),(0,-1),(1,0),(-1,0)])for j,k in c):q+=(b-{*c},C+[[(x-G(c,0),y-G(c,1))for x,y in c]],n-1),
  16. def f(a,b):
  17. c=0
  18. while c:=c+1:
  19. if any(all(O(J)&O(K)for J,K in zip(x,y))for x in P(S(a),c)for y in P(S(b),c)):return c
  20.  
  21. s1 = """
  22. .....
  23. .###.
  24. .#.#.
  25. .###.
  26. .....
  27. """
  28. s1_1 = """
  29. ###..
  30. ..#..
  31. ..#..
  32. ..###
  33. .....
  34. """
  35. s2="""
  36. #...
  37. ##..
  38. .#..
  39. .##.
  40. """
  41. s2_1="""
  42. .##.
  43. ####
  44. ....
  45. ...."""
  46. s3="""
  47. #....#
  48. ######
  49. """
  50. s3_1="""
  51. .####.
  52. .####."""
  53.  
  54. s4=""".###.
  55. ..#..
  56. """
  57. s4_1=""".##..
  58. .##.."""
  59. s5="""##
  60. #."""
  61. s5_1="""
  62. #.
  63. ##"""
  64. s6="""#..
  65. ##.
  66. .##"""
  67. s6_1="""#####"""
  68. def to_board(brd):
  69. return [[int(k == '#') for k in i] for i in filter(None, brd.split('\n'))]
  70.  
  71.  
  72. print(f(to_board(s1), to_board(s1_1)))
  73. print(f(to_board(s2), to_board(s2_1)))
  74. print(f(to_board(s3), to_board(s3_1)))
  75. print(f(to_board(s4), to_board(s4_1)))
  76. print(f(to_board(s5), to_board(s5_1)))
  77. print(f(to_board(s6), to_board(s6_1)))
Success #stdin #stdout 1.06s 14796KB
stdin
Standard input is empty
stdout
2
2
2
2
1
3