Reve's puzzle
From The Canterbury Puzzles:
When the pilgrims were stopping at a wayside tavern, a number of
cheeses of varying sizes caught his alert eye; and calling for four
stools, he told the company that he would show them a puzzle of his
own that would keep them amused during their rest. He then placed n
cheeses of graduating sizes on one of the end stools, the smallest
cheese being at the top (and no cheese resting on a smaller one.)
'This is the riddle', quoth he, 'that I did once set before my fellow
townsmen at Baldeswell, that is in Norfolk, and, by Saint Joce, there
was no man among them that could rede it aright. And yet it is withal
fully easy, for all that I do desire is that, by the moving of one
cheese at a time from one stool unto another, ye shall remove all the
cheeses to the stool at the other end without ever putting any cheese
on one that is smaller than itself. To him that will perform this feat
in the least moves that be possible will I give a draught of the best
that our good host can provide.'
Write a function called reve
that takes five arguments — the number of cheeses n >= 0 and labels for four stools (the source, two intermediaries, and the destination) — and outputs the minimum set of moves necessary to solve Reve's puzzle.
For example, the original puzzle is posed with n = 8, 10, and 21 cheeses, so running reve 8 "A" "B" "C" "D"
should return the following 33 moves:
[("A","C"),("A","B"),("A","D"),("B","D"),("A","B"),("D","A"),("D","B"),("A","B"),("C","B"),("A","C"),("A","D"),("C","D"),("A","C"),("D","A"),("D","C"),("A","C"),("A","D"),("C","D"),("C","A"),("D","A"),("C","D"),("A","C"),("A","D"),("C","D"),("B","A"),("B","D"),("B","C"),("D","C"),("B","D"),("C","B"),("C","D"),("B","D"),("A","D")]
reve 10 "A" "B" "C" "D"
(49 moves)
[("A","D"),("A","B"),("A","C"),("B","C"),("D","C"),("A","B"),("A","D"),("B","D"),("A","B"),("D","A"),("D","B"),("A","B"),("C","A"),("C","D"),("C","B"),("D","B"),("A","B"),("A","C"),("A","D"),("C","D"),("A","C"),("D","A"),("D","C"),("A","C"),("A","D"),("C","D"),("C","A"),("D","A"),("C","D"),("A","C"),("A","D"),("C","D"),("B","C"),("B","D"),("B","A"),("D","A"),("C","A"),("B","D"),("B","C"),("D","C"),("B","D"),("C","B"),("C","D"),("B","D"),("A","B"),("A","C"),("A","D"),("C","D"),("B","D")]
reve 21 "A" "B" "C" "D"
(321 moves)
[("A","C"),("A","D"),("A","B"),("D","B"),("C","B"),("A","D"),("A","C"),("D","C"),("A","D"),("C","A"),("C","D"),("A","D"),("B","A"),("B","C"),("B","D"),("C","D"),("A","D"),("A","B"),("A","C"),("B","C"),("A","B"),("C","A"),("C","B"),("A","B"),("A","C"),("B","C"),("B","A"),("C","A"),("B","C"),("A","B"),("A","C"),("B","C"),("D","B"),("D","C"),("D","A"),("C","A"),("B","A"),("D","C"),("D","B"),("C","B"),("D","C"),("B","D"),("B","C"),("D","C"),("A","D"),("A","B"),("A","C"),("B","C"),("D","C"),("A","B"),("A","D"),("B","D"),("A","B"),("D","A"),("D","B"),("A","B"),("A","D"),("B","D"),("B","A"),("D","A"),("B","D"),("A","B"),("A","D"),("B","D"),("A","B"),("D","A"),("D","B"),("A","B"),("D","A"),("B","D"),("B","A"),("D","A"),("D","B"),("A","B"),("A","D"),("B","D"),("A","B"),("D","A"),("D","B"),("A","B"),("C","B"),("C","A"),("C","D"),("A","D"),("B","D"),("C","A"),("C","B"),("A","B"),("C","A"),("B","C"),("B","A"),("C","A"),("D","C"),("D","B"),("D","A"),("B","A"),("C","A"),("C","D"),("C","B"),("D","B"),("C","D"),("B","C"),("B","D"),("C","D"),("C","B"),("D","B"),("D","C"),("B","C"),("D","B"),("C","D"),("C","B"),("D","B"),("A","D"),("A","B"),("A","C"),("B","C"),("D","C"),("A","B"),("A","D"),("B","D"),("A","B"),("D","A"),("D","B"),("A","B"),("C","A"),("C","D"),("C","B"),("D","B"),("A","B"),("A","C"),("A","D"),("C","D"),("A","C"),("D","A"),("D","C"),("A","C"),("A","D"),("C","D"),("C","A"),("D","A"),("C","D"),("A","C"),("A","D"),("C","D"),("A","C"),("D","A"),("D","C"),("A","C"),("D","A"),("C","D"),("C","A"),("D","A"),("D","C"),("A","C"),("A","D"),("C","D"),("A","C"),("D","A"),("D","C"),("A","C"),("A","D"),("C","D"),("C","A"),("D","A"),("C","D"),("A","C"),("A","D"),("C","D"),("C","A"),("D","A"),("D","C"),("A","C"),("D","A"),("C","D"),("C","A"),("D","A"),("C","D"),("A","C"),("A","D"),("C","D"),("A","C"),("D","A"),("D","C"),("A","C"),("A","D"),("C","D"),("C","A"),("D","A"),("C","D"),("A","C"),("A","D"),("C","D"),("B","A"),("B","C"),("B","D"),("C","D"),("A","D"),("B","C"),("B","A"),("C","A"),("B","C"),("A","B"),("A","C"),("B","C"),("D","B"),("D","A"),("D","C"),("A","C"),("B","C"),("B","D"),("B","A"),("D","A"),("B","D"),("A","B"),("A","D"),("B","D"),("B","A"),("D","A"),("D","B"),("A","B"),("D","A"),("B","D"),("B","A"),("D","A"),("C","D"),("C","A"),("C","B"),("A","B"),("D","B"),("C","A"),("C","D"),("A","D"),("C","A"),("D","C"),("D","A"),("C","A"),("B","C"),("B","D"),("B","A"),("D","A"),("C","A"),("B","D"),("B","C"),("D","C"),("B","D"),("C","B"),("C","D"),("B","D"),("B","C"),("D","C"),("D","B"),("C","B"),("D","C"),("B","D"),("B","C"),("D","C"),("B","D"),("C","B"),("C","D"),("B","D"),("C","B"),("D","C"),("D","B"),("C","B"),("C","D"),("B","D"),("B","C"),("D","C"),("B","D"),("C","B"),("C","D"),("B","D"),("A","D"),("A","B"),("A","C"),("B","C"),("D","C"),("A","B"),("A","D"),("B","D"),("A","B"),("D","A"),("D","B"),("A","B"),("C","A"),("C","D"),("C","B"),("D","B"),("A","B"),("A","C"),("A","D"),("C","D"),("A","C"),("D","A"),("D","C"),("A","C"),("A","D"),("C","D"),("C","A"),("D","A"),("C","D"),("A","C"),("A","D"),("C","D"),("B","C"),("B","D"),("B","A"),("D","A"),("C","A"),("B","D"),("B","C"),("D","C"),("B","D"),("C","B"),("C","D"),("B","D"),("A","B"),("A","C"),("A","D"),("C","D"),("B","D")]
An up vote for all successful attempts. Shortest solution by character count wins. Sorry, no beer. Although if you're ever in the Boston area, it's on me.
Questions
- Is this a suitable question for code golf?
- Is it clear what I'm asking? Do I need to be more specific about input/output, etc.?
- Any other suggestions?