Thanks to some help from people here, I was able to get my code for Tasmanian camels puzzle working. However, it is horribly slow (I think. I\'m not sure because this is my
The code below is using less than 1 second to solve this:
from itertools import permutations
GAP='G'
LEFT='F'
RIGHT='B'
BEGIN=('F','F','F','F','G','B','B','B','B')
END=('B','B','B','B','G','F','F','F','F')
LENGTH=len(BEGIN)
ALL=set(permutations(BEGIN))
def NextMove(seq):
g=seq.index(GAP)
ret = set()
def swap(n):
return seq[:n]+seq[n:n+2][::-1]+seq[n+2:]
if g>0 and seq[g-1]==LEFT:
ret.add(swap(g-1))
if g1 and seq[g-1]==RIGHT and seq[g-2]==LEFT:
ret.add(seq[:g-2]+seq[g:g+1]+seq[g-1:g]+seq[g-2:g-1]+seq[g+1:])
return ret
AllMoves={state:NextMove(state) for state in ALL}
def Solve(now,target):
if now==target:
return True
for state in AllMoves[now]:
if Solve(state,target):
print(now)
return True
return False
Solve(BEGIN,END)