Solve the Tower of Hanoi · Compiler

Solve the Tower of Hanoi

A stack of n disks starts on peg A, largest at the bottom. Move the stack to C using B as the spare peg. Only a top disk may move, onto an empty peg or a larger disk. To transfer a stack, recursively move its upper n - 1 disks to the spare peg, move its bottom disk to the destination, then transfer the upper stack onto it. Moving zero disks takes no moves. Identify the kth move.

Input
One line contains two integers, the disk count n (1 to 15) and the move number k (1 through (2^n) - 1, where ^ denotes exponentiation). Count moves from 1 and number disks from 1 (smallest) to n (largest).
Output
Print three space-separated tokens giving the disk number, source peg and destination peg of move k. Use the uppercase peg names A, B and C.

Compiler Explorer runs your code when you run or check it.

Loading your exercise draft…