# Tower of Hanoi **Transfer N disks between three pegs following size constraints** --- ## Overview The Tower of Hanoi is a classic recursive puzzle consisting of three pegs (labeled A, B, and C) and N disks of different sizes, numbered from 1 (smallest) to N (largest). This puzzle is famous in computer science for demonstrating recursion and exponential time complexity. ### Difficulty Rating: ⭐⭐⭐⭐⭐ (Very Hard - Exponential) --- ## 📊 Statistics | Metric | Value | |--------|-------| | **Total Puzzles** | 60 | | **Total Moves** | 12,216 | | **Training Puzzles (N=1-7)** | 42 | | **Test Puzzles (N=8-10)** | 18 | | **Difficulty Parameter** | N (number of disks) | | **Number of Pegs** | 3 (A, B, C) | | **Solution Length** | L(N) = **2^N - 1** (exponential!) | | **Transition Locality** | O(N) - must check top disk constraints | --- ## 🎯 Puzzle Rules ### Objective Transfer all N disks from a designated **start peg** to a **target end peg** while maintaining size ordering (largest at bottom, smallest at top) throughout all intermediate states. ### Constraints 1. **Single Disk Movement**: Only one disk may be moved at a time 2. **Top Disk Access**: Only the topmost disk from any peg can be selected for movement 3. **Size Ordering Constraint**: A larger disk may **never** be placed on top of a smaller disk ### Why Tower of Hanoi is Extremely Challenging Tower of Hanoi is the **hardest** puzzle in the RecurrReason benchmark: 1. **Exponential Solution Length**: L(N) = 2^N - 1 - N=3: 7 moves - N=7: 127 moves - N=10: **1,023 moves!** 2. **Recursive Structure**: Optimal solution requires decomposing problem recursively: - Move top N-1 disks to auxiliary peg - Move largest disk to target peg - Move N-1 disks from auxiliary to target peg 3. **Compounding Errors**: With per-step error rate ε, success probability is: ``` P(success) ≈ (1-ε)^(2^N - 1) → 0 as N grows ``` --- ## 📋 State Representation States are represented as **lists of three lists**, where each list represents one peg (A, B, C) containing disks ordered from **top to bottom**. ### Format ```python [[1, 2, 3], [], []] ``` This represents: - **Peg A**: Disks 1 (top), 2, 3 (bottom) - **Peg B**: Empty - **Peg C**: Empty **Important**: Disks are numbered 1 (smallest) to N (largest). ### Move Representation ```python [1, 'A', 'B'] ``` This represents: **Move disk 1 from peg A to peg B** Format: `[disk_number, source_peg, destination_peg]` --- ## 🖼️ Example Puzzle ![Tower of Hanoi Example](https://github.com/gowravmannem/Recurrent-Reasoning-on-Puzzles/blob/main/assets/tower_of_hanoi.png?raw=true) ### Example Trajectory (N=2) **Initial State**: `[[], [], [1, 2]]` (both disks on peg C) **Goal State**: `[[], [1, 2], []]` (both disks on peg B) **Start Peg**: C **Goal Peg**: B **Optimal Solution Length**: 3 moves (2^2 - 1 = 3) **Step-by-step solution:** | Step | Current State | Next State | Move | Description | |------|--------------|-----------|------|-------------| | 0 | `[[], [], [1, 2]]` | `[[1], [], [2]]` | `[1, 'C', 'A']` | Move disk 1 from C to A | | 1 | `[[1], [], [2]]` | `[[1], [2], []]` | `[2, 'C', 'B']` | Move disk 2 from C to B | | 2 | `[[1], [2], []]` | `[[], [1, 2], []]` | `[1, 'A', 'B']` | Move disk 1 from A to B | | 3 | `[[], [1, 2], []]` | `[[], [1, 2], []]` | `['_', '_', '_']` | Goal reached! | ### Recursive Pattern The recursive pattern for N disks: ``` function HANOI(n, source, target, auxiliary): if n == 1: move disk 1 from source to target else: HANOI(n-1, source, auxiliary, target) # Move n-1 to aux move disk n from source to target # Move largest HANOI(n-1, auxiliary, target, source) # Move n-1 to target ``` --- ## 📁 CSV Column Descriptions ### Columns | Column | Type | Description | |--------|------|-------------| | `N` | int | Number of disks (difficulty parameter) | | `start_state` | string | Initial configuration of all three pegs | | `goal_state` | string | Target configuration to achieve | | `start_peg` | string | Starting peg ('A', 'B', or 'C') | | `goal_peg` | string | Target peg ('A', 'B', or 'C') | | `current_state` | string | State before this move | | `next_state` | string | State after applying this move | | `move` | string | Action taken: `[disk, source_peg, dest_peg]` | | `num_moves` | int | Total moves in optimal solution (2^N - 1) | ### Data Format Each row represents one **move** in a solution trajectory. **Example CSV rows:** ```csv N,start_state,goal_state,start_peg,goal_peg,current_state,next_state,move,num_moves 2,"[[],[],[1,2]]","[[],[1,2],[]]",C,B,"[[],[],[1,2]]","[[1],[],[2]]","[1,'C','A']",3 2,"[[],[],[1,2]]","[[],[1,2],[]]",C,B,"[[1],[],[2]]","[[1],[2],[]]","[2,'C','B']",3 2,"[[],[],[1,2]]","[[],[1,2],[]]",C,B,"[[1],[2],[]]","[[],[1,2],[]]","[1,'A','B']",3 2,"[[],[],[1,2]]","[[],[1,2],[]]",C,B,"[[],[1,2],[]]","[[],[1,2],[]]","['_','_','_']",3 ``` --- ## 💡 Usage Tips ### For Model Training ⚠️ **Warning**: Tower of Hanoi is **difficult** for current sequence models. Suggested approaches: 1. **Add explicit subgoal markers**: Annotate when recursive subproblems start/end 2. **Hierarchical representations**: Encode recursive structure explicitly 3. **Search augmentation**: Use beam search or MCTS during decoding 4. **Curriculum learning**: Start with N=1, slowly increase (but likely still fails at N≥3) ### For Evaluation ```python from datasets import load_dataset # Load Tower of Hanoi dataset = load_dataset("gmannem/RecurrReason", "tower_of_hanoi") # WARNING: Expect very low success rates! # Models typically solve only N=1 def evaluate_hanoi(model, example): """ Evaluation with strict constraints. A single size-ordering violation = immediate failure. """ current = example['start_state'] goal = example['goal_state'] steps = 0 max_steps = 2 * example['num_moves'] # 2 × (2^N - 1) while steps < max_steps: next_state = model.predict(current, goal) # Check size ordering (CRITICAL!) if violates_size_constraint(next_state): return "INVALID_MOVE", steps if next_state == goal: return "SUCCESS", steps current = next_state steps += 1 return "TIMEOUT", steps def violates_size_constraint(state): """Check if any peg has larger disk on top of smaller.""" for peg in state: for i in range(len(peg) - 1): if peg[i] > peg[i+1]: # Larger disk on top! return True return False ``` --- ## 🔬 Research Directions Tower of Hanoi poses fundamental challenges for sequence models: 1. **Hierarchical Planning**: How to encode recursive subgoals? 2. **Search Integration**: Can we augment models with A* or MCTS? 3. **Neuro-Symbolic Approaches**: Combine neural prediction with symbolic constraint checking 4. **Explicit Memory**: External memory to track subproblem state 5. **Length Generalization**: Current models cannot extrapolate from short to long sequences on this task **Key Insight**: Success on Tower of Hanoi likely requires **search** or **explicit hierarchical representations**, not just larger models. --- ## 📚 References **Main Paper:** ```bibtex @inproceedings{mannem2026recurrent, title={Recurrent Reasoning on Symbolic Puzzles with Sequence Models}, author={Gowrav Mannem and Chowdhury Marzia Mahjabin and Jason Chen and Shivank Garg and Kevin Zhu}, booktitle={ICLR 2026 Workshop on Logical Reasoning of Large Language Models}, year={2026} } ``` **Classic Reference:** ```bibtex @article{lucas1883tower, title={Récréations mathématiques}, author={Lucas, Édouard}, journal={Gauthier-Villars}, year={1883} } ``` --- [← Back to Main README](README.md)