from collections import deque
# The only 2 things you modifiy in this script is
# "flip_map" and "initial_active"
#EXAMPLE ROW IN FLIP_MAP
# 1: [4, 6, 10, 11],
flip_map = {
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: [],
}
initial_active = []
def flip_pieces(state, piece):
new_state = state
new_state ^= (1 << (piece - 1))
for p in flip_map[piece]:
new_state ^= (1 << (p - 1))
return new_state
def check_win(state):
return state == 0
# BFS to find the solution
def solve_puzzle(initial_state):
queue = deque([(initial_state, [])]) # Queue holds tuples of (state, move sequence)
visited = set()
visited.add(initial_state)
while queue:
current_state, move_sequence = queue.popleft()
if check_win(current_state):
return move_sequence
# For each possible move (i.e., each active piece), flip the state and continue
for i in range(25):
if current_state & (1 << i): # Check if the piece is active (bit is 1)
new_state = flip_pieces(current_state, i + 1)
if new_state not in visited:
visited.add(new_state)
queue.append((new_state, move_sequence + [i + 1]))
return None
initial_state = 0
for piece in initial_active:
initial_state |= (1 << (piece - 1))
solution = solve_puzzle(initial_state)
if solution:
print("Solution found! Moves sequence:")
print(solution)
else:
print("No solution found.")
if __name__ == "__main__":
solve_puzzle(initial_state)