Card-handling puzzle · optimal solver

Two-Pile Shuffle

The deck starts in letter order, A on top, then B, C, D… Each card has a number written on the bottom, and the goal is a deck that reads 1, 2, 3… from the top. Each round you deal every card from the top of the deck onto a left or right pile, then set the right pile on top of the left pile. The solver finds the fewest rounds that get there, and the exact pile for every card in every round.

Goal deck · top card first · the numbers read 1 to n · color = block · L/R = pile in each round

Dealing instructions

How the solver works

1 · Read one round backwards

Each card goes on top of its pile, so a pile ends up in reverse deal order, and the right pile lands on top. Turn the new deck over and you get the left cards in the order they were dealt, then the right cards in the order they were dealt. One round is a stable split followed by turning the deck over.

2 · Move every flip to the front

Turning a deck over and then splitting it gives the same result as splitting first with left and right swapped, then turning it over. Pushing all the flips to the start shows that k rounds equal: reverse the start deck if k is odd, then do k stable splits. Repeated stable splits are a least-significant-digit radix sort. Each card gets a k-bit label and the deck ends up sorted by label, with ties kept in their earlier order.

3 · Count runs in the target

A stable sort by label produces the target exactly when the target cuts into consecutive blocks, where each block already appears in the same relative order in the start deck (reversed for odd k). Each block gets one label. The solver walks the target from the top and opens a new block whenever a card sat above the previous card in the start deck. With k bits there are 2k labels, so the answer is the smallest k with blocks ≤ 2^k.

4 · Why nothing can do better

Any k rounds amount to a stable sort by a k-bit label, and that can never produce more than 2k blocks. The block count is a hard lower bound and the solver meets it. A breadth-first search over all 5,913 orderings of decks with up to 7 cards agrees with it on every one. Worst case: 1 round for 2–3 cards, 2 for 4–6, 3 for 7–12, 4 for 13–24, 5 for 25–48, 6 for 49–96. In general ⌊log₂(4(n−1)/3)⌋ rounds, about log₂ n + 0.4. A shuffled 52-card deck usually needs 5; it needs 6 only when its descent count lands in a narrow band.

solve(start, target):                       # lists are top card first
  if target == start: return 0 rounds
  for k = 1, 2, 3, …:
    base   = reverse(start) if k is odd else start
    blocks = split target into maximal runs that keep base's order
    if count(blocks) ≤ 2^k: break
  for each card in block j:
    label = (2^k − 1) − j                   # first block gets the highest label
    for round i = 1 … k:
      bit = (label >> (i−1)) & 1
      if (k − i) is even: bit = 1 − bit     # the deck flips every round
      pile[card][i] = LEFT if bit else RIGHT

Cost: one pass over the target per candidate k, so O(n log n) for the whole plan. Dealing takes k·n card moves plus k pile merges.