I'm glad we could work out a compromise. Below is my answer as I
originally intended to post it.
Dear alexinia,
I solved this problem by writing a Python script that simulates every possible
sequence of moves. If you'd like to run the script, you'll have to download and
install a recent version of the Python interpreter, unless you have one already.
python.org: Download Standard Python Software
http://www.python.org/download/
It turns out that exactly 24 moves are required to swap the a's and b's. There a
re two ways to do so, one of which is the following.
aaaa bbbb initial state
123456789
aaa abbbb 1. 'a' slides from 4 to 5
123456789
aaaba bbb 2. 'b' jumps from 6 to 4
123456789
aaabab bb 3. 'b' slides from 7 to 6
123456789
aaab babb 4. 'a' jumps from 5 to 7
123456789
aa bababb 5. 'a' jumps from 3 to 5
123456789
a abababb 6. 'a' slides from 2 to 3
123456789
aba ababb 7. 'b' jumps from 4 to 2
123456789
ababa abb 8. 'b' jumps from 6 to 4
123456789
abababa b 9. 'b' jumps from 8 to 6
123456789
abababab 10. 'b' slides from 9 to 8
123456789
ababab ba 11. 'a' jumps from 7 to 9
123456789
abab baba 12. 'a' jumps from 5 to 7
123456789
ab bababa 13. 'a' jumps from 3 to 5
123456789
babababa 14. 'a' jumps from 1 to 3
123456789
b abababa 15. 'b' slides from 2 to 1
123456789
bba ababa 16. 'b' jumps from 4 to 2
123456789
bbaba aba 17. 'b' jumps from 6 to 4
123456789
bbababa a 18. 'b' jumps from 8 to 6
123456789
bbabab aa 19. 'a' slides from 7 to 8
123456789
bbab baaa 20. 'a' jumps from 5 to 7
123456789
bb babaaa 21. 'a' jumps from 3 to 5
123456789
bbb abaaa 22. 'b' slides from 4 to 3
123456789
bbbba aaa 23. 'b' jumps from 6 to 4
123456789
bbbb aaaa 24. 'a' slides from 5 to 6
123456789
The Python script follows below.
Cheers,
leapinglizard
#------ begin slider.py
import sys, os, string, re
class Coins:
def __init__(self, start_state):
self.start_state = start_state
self.end_state = ''
for i in range(len(start_state)-1, -1, -1):
self.end_state += start_state[i]
self.best = -1
self.seek(start_state, 0, [])
def disp_board(self, board, comment):
tab = 4*' '
label = 10*'1234567890'
print '%s%s%s%s' % (tab, board, tab, comment)
print '%s%s' % (tab, label[:len(board)])
print
def swapped(self, board, a_pos, b_pos):
if a_pos == b_pos:
return board
if a_pos > b_pos:
(a, b) = (b_pos, a_pos)
else:
(a, b) = (a_pos, b_pos)
ac = board[a]
bc = board[b]
return board[:a] + board[b] + board[a+1:b] + board[a] + board[b+1:]
def seek(self, board, depth, history):
if board == self.end_state:
if self.best == -1 or depth < self.best:
print 'found a solution in %d moves' % depth
self.best = depth
board = self.start_state
self.disp_board(board, 'initial state')
i = 0
for code in history:
(action, from_pos, to_pos) = code
i += 1
move = '%d. \'%s\' %ss from %d to %d' % (
i, board[from_pos], action, from_pos+1, to_pos+1)
board = self.swapped(board, from_pos, to_pos)
self.disp_board(board, move)
return
for i in range(len(board)):
c = board[i].lower()
if c == 'a' and i < len(board)-1 and board[i+1] == ' ':
self.seek(self.swapped(board, i, i+1), depth+1,
history+[('slide', i, i+1)])
elif c == 'a' and i < len(board)-2 and board[i+1:i+3] == 'b ':
self.seek(self.swapped(board, i, i+2), depth+1,
history+[('jump', i, i+2)])
elif c == 'b' and i > 0 and board[i-1] == ' ':
self.seek(self.swapped(board, i, i-1), depth+1,
history+[('slide', i, i-1)])
elif c == 'b' and i > 1 and board[i-2:i] == ' a':
self.seek(self.swapped(board, i, i-2), depth+1,
history+[('jump', i, i-2)])
if __name__ == '__main__':
Coins('aaaa bbbb')
#------ end slider.py |