Moving the Wedding Cake
A bakery's wedding cake is built from separate round tiers, biggest at the bottom, each smaller than the one below. It sits on stand A, the photographer wants it on stand C, and stand B is a spare.
The tiers are heavy and soft. You can lift only one tier at a time, always a stand's top tier, and never set a tier on a smaller one or it gets crushed. Plan the move.
Your task
Write moveCake(tiers). The cake has tiers tiers, all on stand A. Return the list of moves, in order, that gets the whole cake onto stand C.
Write each move as two letters: the stand you lift from, then the stand you place on. "AC" means: take A's top tier and place it on C. Every move must be legal (lift from a stand that has a tier, never place a tier on a smaller one), and the list must use the fewest moves possible. With 0 tiers, return an empty list.
moveCake(tiers: int) → string[] Several answers can be correct; any valid one is accepted.
Examples
tiers = 1
["AC"]
With one tier, you lift it from A and place it on C, which takes one move.
tiers = 2
["AB", "AC", "BC"]
The big tier cannot go to C while the small one sits on it. So the small tier goes to spare B, the big tier goes A → C, and the small tier goes from B onto it. Putting the small tier on C first would block C for good.
Constraints
- 0 ≤ tiers ≤ 12
- Stands are named "A" (start), "B" (spare) and "C" (goal).
- The shortest plan is unique, so there is exactly one correct list.
Start with brute force
Treat every arrangement of tiers on the three stands as a state and search breadth first until every tier is on C. That finds the shortest plan, but each tier can be on any of three stands: 3n states, 531,441 for 12 tiers.
It still finishes at this size, but it is heavy and hides the pattern. The plan itself has 2n − 1 moves, so nothing beats writing those down. A good solution does a fixed amount of work per move and uses almost no other memory.
Hints
0 of 4 shown- Hint 1
Try 2 tiers by hand. Before the big bottom tier can move to C, where must the small tier be?
- Hint 2
When the biggest tier moves from A to C, C must be empty and A holds only that tier. Where are all the other tiers?
- Hint 3
They are all on B. So moving n tiers takes three parts: move the top n − 1 tiers from A to B, move the biggest from A to C, then move the n − 1 tiers from B to C. Each n − 1 part is the same puzzle with the stands in different roles.
- Hint 4
Write a helper
move(count, from, to, spare)that adds to one shared list. Ifcountis 0, do nothing. Otherwise: movecount − 1tiers fromfromtospare(withtoas spare), add the movefrom + to, then movecount − 1tiers fromsparetoto(withfromas spare).
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
Look only at the biggest tier. It can move to C only when C is empty and nothing sits on it, so every other tier must be parked on B. That one fact gives the plan:
- Move the top n − 1 tiers from A to B.
- Move the biggest tier from A to C.
- Move the n − 1 tiers from B to C, on top of it.
Steps 1 and 3 are the same puzzle with one tier fewer, so the function calls itself; only the stands' roles change. Trace 3 tiers:
- Move 2 tiers A → B, with C as spare:
AC,AB,CB. (The 2-tier answer with B and C swapped.) - Move the biggest tier:
AC. - Move 2 tiers B → C, with A as spare:
BA,BC,AC.
That is 7 moves in total. In general, moves(n) = 2 × moves(n − 1) + 1 = 2n − 1. A common slip is passing the stands in the wrong order to the inner calls; read each call aloud as "move this many tiers from here to there, using that as the spare" and check it matches the plan.
function moveCake(tiers) {
const moves = [];
function move(count, from, to, spare) {
if (count === 0) return; // nothing to carry
move(count - 1, from, spare, to); // park the smaller tiers
moves.push(from + to); // carry the biggest one
move(count - 1, spare, to, from); // stack the smaller tiers on top
}
move(tiers, 'A', 'C', 'B');
return moves;
}def move_cake(tiers):
moves = []
def move(count, src, dst, spare):
if count == 0:
return # nothing to carry
move(count - 1, src, spare, dst) # park the smaller tiers
moves.append(src + dst) # carry the biggest one
move(count - 1, spare, dst, src) # stack the smaller tiers on top
move(tiers, 'A', 'C', 'B')
return movesRun your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.