fonkensemble

Untitled

Jul 24th, 2025
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.35 KB | None | 0 0
  1.  
  2. # red (a1, a2, b1, b2)
  3. red = {(0,0),(1,0),(0,1),(1,1)}
  4. # magenta (a4, a5, b4, b5)
  5. magenta = {(3,0),(4,0),(3,1),(4,1)}
  6. # cyan (d1, d2, e1, e2)
  7. cyan = {(0,3),(1,3),(0,4),(1,4)}
  8. # yellow (d4, d5, e4, e5)
  9. yellow = {(3,3),(4,3),(3,4),(4,4)}
  10. # green (a3, b3, c1, c2, c3, c4, c5, d3, e3)
  11. green = {(2,0),(2,1),(0,2),(1,2),(2,2),(3,2),(4,2),(2,3),(2,4)}
  12.  
  13. # 0 1 2 3 4
  14. # +---+---+---+---+---+
  15. # 0 | 1 | 0 | 0 | 1 | 1 | 1
  16. # +---+---+---+---+---+
  17. # 1 | 1 | 1 | 0 | 1 | 0 | 2
  18. # +---+---+---+---+---+
  19. # 2 | 0 | 1 | 0 | 1 | 0 | 3
  20. # +---+---+---+---+---+
  21. # 3 | 1 | 1 | 1 | 0 | 0 | 4
  22. # +---+---+---+---+---+
  23. # 4 | 1 | 1 | 0 | 1 | 1 | 5
  24. # +---+---+---+---+---+
  25. # A B C D E
  26.  
  27.  
  28. def cell_from_index(index):
  29. """
  30. Convert a linear index to a grid cell (x, y).
  31. """
  32. return (index // 5, index % 5)
  33.  
  34.  
  35. solved_grid = [
  36. [1, 0, 0, 1, 1], # Row 0
  37. [1, 1, 0, 1, 0], # Row 1
  38. [0, 1, 0, 1, 0], # Row 2
  39. [1, 1, 1, 0, 0], # Row 3
  40. [1, 1, 0, 1, 1] # Row 4
  41. ]
  42.  
  43. flips = [
  44.  
  45. [
  46. #a1 -> (a1, a2, a4, a5, b4).
  47. [(0,0), (1,0), (3,0), (4,0), (3,1)],
  48. #b1 -> (a5, b1, b3, d2, d4).
  49. [(4,0), (0,1), (2,1), (1,3), (3,3)],
  50. #c1 -> (a5, c1, c4, c5, d3).
  51. [(4,0), (0,2), (3,2), (4,2), (2,3)],
  52. #d1 -> (a1, a4, c1, d1, d2).
  53. [(0,0), (3,0), (0,2), (0,3), (1,3)],
  54. #e1 -> (a3, c3, e1, e3, e5).
  55. [(2,0), (2,2), (0,4), (2,4), (4,4)]
  56. ],
  57. [
  58. #a2 -> (a2, b3. d3, e2, e4).
  59. [(1,0), (2,1), (2,3), (0,4), (3,4)],
  60. #b2 -> (b2, b4, c5, e2, e5).
  61. [(1,1), (3,1), (4,2), (1,4), (4,4)],
  62. #c2 -> (a3, c2, d4, e1, e3).
  63. [(2,0), (1,2), (3,3), (0,4), (2,4)],
  64. #d2 -> (a1, a4, d1, d2, d5).
  65. [(0,0), (3,0), (0,3), (1,3), (4,3)],
  66. #e2 -> (b2, c1, c3, d2, e2).
  67. [(1,1), (0,2), (2,2), (1,3), (1,4)]
  68. ],
  69. [
  70. #a3 -> (a3, c3, c5, e3, e4).
  71. [(2,0), (2,2), (4,2), (2,4), (3,4)],
  72. #b3 -> (a2, a4, b3, b5, d3).
  73. [(1,0), (3,0), (2,1), (4,1), (2,3)],
  74. #c3 -> (b1, b2, c3, e3, e5).
  75. [(0,1), (1,1), (2,2), (2,4), (4,4)],
  76. #d3 -> (a2, a3, c4, d2, d3).
  77. [(1,0), (2,0), (3,2), (1,3), (2,3)],
  78. #e3 -> (a1, a3, b2, e2, e3).
  79. [(0,0), (2,0), (1,1), (1,4), (2,4)],
  80. ],
  81. [
  82. #a4 -> (a4, b1, c3, c4, d4).
  83. [(3,0), (0,1), (2,2), (3,2), (3,3)],
  84. #b4 -> (a5, b4, c1, c2, e1).
  85. [(4,0), (3,1), (0,2), (1,2), (0,4)],
  86. #c4 -> (a2, b1, c4, e1, e4).
  87. [(1,0), (0,1), (3,2), (0,4), (3,4)],
  88. #d4 -> (b5, c4, c5, d4, d5).
  89. [(4,1), (3,2), (4,2), (3,3), (4,3)],
  90. #e4 -> (c2, d1, d4, d5, e4).
  91. [(1,2), (0,3), (3,3), (4,3), (3,4)],
  92. ],
  93. [
  94. #a5 -> (a5, b3, c2, e2, e5).
  95. [(4, 0), (1, 1), (2, 1), (1, 4), (4, 4)],
  96. #b5 -> (b4, b5, c2, d1, d3).
  97. [(3,1), (4,1), (1,2), (0,3), (2,3)],
  98. #c5 -> (b2, b3, c5, d5, e1).
  99. [(1,1), (2,1), (4,2), (4,3), (0,4)],
  100. #d5 -> (b1, b5, d1, d5, e4).
  101. [(0,1), (4,1), (0,3), (4,3), (3,4)],
  102. #e5 -> (a1, b4, b5, c1, e5).
  103. [(0,0), (3,1), (4,1), (0,2), (4,4)]
  104. ]
  105. ]
  106.  
  107. def flip_grid(grid, flip, pressed_cell):
  108. """
  109. Flip the cells in the grid according to the flip pattern.
  110. """
  111. if grid[pressed_cell[0]][pressed_cell[1]] == 1:
  112. return None
  113. # Create a copy of the grid to avoid modifying the original
  114. local_grid = [row[:] for row in grid]
  115. for cell in flip:
  116. x, y = cell
  117. local_grid[x][y] = 1 - local_grid[x][y] # Toggle between 0 and 1
  118. if is_valid(local_grid):
  119. return local_grid
  120. #print_grid(local_grid)
  121. return None
  122.  
  123. def is_valid(grid):
  124. #print_grid(grid)
  125. return any(grid[x][y] == 1 for x,y in red) and \
  126. any(grid[x][y] == 1 for x,y in magenta) and \
  127. any(grid[x][y] == 1 for x,y in cyan) and \
  128. any(grid[x][y] == 1 for x,y in yellow) and \
  129. any(grid[x][y] == 1 for x,y in green)
  130.  
  131. def is_solved(grid):
  132. """
  133. Check if the grid is solved
  134. """
  135. return grid == solved_grid
  136.  
  137. possible_prefixes = []
  138.  
  139. # b1 is first flip
  140. indexes = set([0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24])
  141.  
  142. # todo: for each prefix, generate all possible next prefixes and give them with previous prefix length
  143. # -> can generate the prefix grid and just try the last steps
  144. def generate_prefixes(prefixes):
  145. for prefix in prefixes:
  146. for index in indexes - set(prefix):
  147. yield prefix + [index]
  148.  
  149. def find_solution():
  150. """
  151. Find a solution to the lights out puzzle.
  152. """
  153. from random import shuffle
  154. step = 0
  155. possible_prefixes = [[1]] # Start with the first flip (b1)
  156. while True:
  157. current_prefixes = []
  158. for indexes in generate_prefixes(possible_prefixes):
  159. grid = [[0] * 5 for _ in range(5)] # Initialize a 5x5 grid with all lights off
  160. #input(f"Trying path: {indexes}")
  161. solution = []
  162. bad_prefix = False
  163. for index in indexes:
  164. cell = cell_from_index(index)
  165. #print(f"Flipping cell {cell}")
  166. temp_grid = flip_grid(grid, flips[cell[0]][cell[1]], cell)
  167. if temp_grid is None:
  168. #print(f"Invalid flip at {cell}, skipping.")
  169. bad_prefix = True
  170. break
  171. #print_grid(temp_grid)
  172. grid = temp_grid
  173. solution.append(cell)
  174. if is_solved(grid):
  175. return grid, solution
  176. if not bad_prefix:
  177. current_prefixes.append(indexes)
  178. possible_prefixes = current_prefixes[:]
  179. print(f"{len(possible_prefixes)} prefixes of length {len(possible_prefixes[0])} found.")
  180. return None, None
  181.  
  182. def print_grid(grid):
  183. """
  184. Print the grid in a readable format.
  185. """
  186. for row in grid:
  187. print(" ".join(str(cell) for cell in row))
  188. print()
  189.  
  190. if __name__ == "__main__":
  191. # Find a solution
  192. solved_grid, solution = find_solution()
  193. print(len(possible_prefixes), "possible prefixes found.")
  194.  
  195. if solution:
  196. print("Solution Found:")
  197. print("Flips applied:", solution)
  198. print_grid(solved_grid)
  199. else:
  200. print("No solution found.")
Advertisement
Add Comment
Please, Sign In to add comment