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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Tower of Hanoi
|
| 2 |
+
|
| 3 |
+
**Transfer N disks between three pegs following size constraints**
|
| 4 |
+
|
| 5 |
+
---
|
| 6 |
+
|
| 7 |
+
## Overview
|
| 8 |
+
|
| 9 |
+
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.
|
| 10 |
+
|
| 11 |
+
### Difficulty Rating: ⭐⭐⭐⭐⭐ (Very Hard - Exponential)
|
| 12 |
+
|
| 13 |
+
---
|
| 14 |
+
|
| 15 |
+
## 📊 Statistics
|
| 16 |
+
|
| 17 |
+
| Metric | Value |
|
| 18 |
+
|--------|-------|
|
| 19 |
+
| **Total Puzzles** | 60 |
|
| 20 |
+
| **Total Moves** | 12,216 |
|
| 21 |
+
| **Training Puzzles (N=1-7)** | 42 |
|
| 22 |
+
| **Test Puzzles (N=8-10)** | 18 |
|
| 23 |
+
| **Difficulty Parameter** | N (number of disks) |
|
| 24 |
+
| **Number of Pegs** | 3 (A, B, C) |
|
| 25 |
+
| **Solution Length** | L(N) = **2^N - 1** (exponential!) |
|
| 26 |
+
| **Transition Locality** | O(N) - must check top disk constraints |
|
| 27 |
+
|
| 28 |
+
---
|
| 29 |
+
|
| 30 |
+
## 🎯 Puzzle Rules
|
| 31 |
+
|
| 32 |
+
### Objective
|
| 33 |
+
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.
|
| 34 |
+
|
| 35 |
+
### Constraints
|
| 36 |
+
1. **Single Disk Movement**: Only one disk may be moved at a time
|
| 37 |
+
2. **Top Disk Access**: Only the topmost disk from any peg can be selected for movement
|
| 38 |
+
3. **Size Ordering Constraint**: A larger disk may **never** be placed on top of a smaller disk
|
| 39 |
+
|
| 40 |
+
### Why Tower of Hanoi is Extremely Challenging
|
| 41 |
+
|
| 42 |
+
Tower of Hanoi is the **hardest** puzzle in the RecurrReason benchmark:
|
| 43 |
+
|
| 44 |
+
1. **Exponential Solution Length**: L(N) = 2^N - 1
|
| 45 |
+
- N=3: 7 moves
|
| 46 |
+
- N=7: 127 moves
|
| 47 |
+
- N=10: **1,023 moves!**
|
| 48 |
+
|
| 49 |
+
2. **Recursive Structure**: Optimal solution requires decomposing problem recursively:
|
| 50 |
+
- Move top N-1 disks to auxiliary peg
|
| 51 |
+
- Move largest disk to target peg
|
| 52 |
+
- Move N-1 disks from auxiliary to target peg
|
| 53 |
+
|
| 54 |
+
3. **Compounding Errors**: With per-step error rate ε, success probability is:
|
| 55 |
+
```
|
| 56 |
+
P(success) ≈ (1-ε)^(2^N - 1) → 0 as N grows
|
| 57 |
+
```
|
| 58 |
+
|
| 59 |
+
---
|
| 60 |
+
|
| 61 |
+
## 📋 State Representation
|
| 62 |
+
|
| 63 |
+
States are represented as **lists of three lists**, where each list represents one peg (A, B, C) containing disks ordered from **top to bottom**.
|
| 64 |
+
|
| 65 |
+
### Format
|
| 66 |
+
```python
|
| 67 |
+
[[1, 2, 3], [], []]
|
| 68 |
+
```
|
| 69 |
+
|
| 70 |
+
This represents:
|
| 71 |
+
- **Peg A**: Disks 1 (top), 2, 3 (bottom)
|
| 72 |
+
- **Peg B**: Empty
|
| 73 |
+
- **Peg C**: Empty
|
| 74 |
+
|
| 75 |
+
**Important**: Disks are numbered 1 (smallest) to N (largest).
|
| 76 |
+
|
| 77 |
+
### Move Representation
|
| 78 |
+
```python
|
| 79 |
+
[1, 'A', 'B']
|
| 80 |
+
```
|
| 81 |
+
|
| 82 |
+
This represents: **Move disk 1 from peg A to peg B**
|
| 83 |
+
|
| 84 |
+
Format: `[disk_number, source_peg, destination_peg]`
|
| 85 |
+
|
| 86 |
+
---
|
| 87 |
+
|
| 88 |
+
## 🖼️ Example Puzzle
|
| 89 |
+
|
| 90 |
+

|
| 91 |
+
|
| 92 |
+
### Example Trajectory (N=2)
|
| 93 |
+
|
| 94 |
+
**Initial State**: `[[], [], [1, 2]]` (both disks on peg C)
|
| 95 |
+
**Goal State**: `[[], [1, 2], []]` (both disks on peg B)
|
| 96 |
+
**Start Peg**: C
|
| 97 |
+
**Goal Peg**: B
|
| 98 |
+
**Optimal Solution Length**: 3 moves (2^2 - 1 = 3)
|
| 99 |
+
|
| 100 |
+
**Step-by-step solution:**
|
| 101 |
+
|
| 102 |
+
| Step | Current State | Next State | Move | Description |
|
| 103 |
+
|------|--------------|-----------|------|-------------|
|
| 104 |
+
| 0 | `[[], [], [1, 2]]` | `[[1], [], [2]]` | `[1, 'C', 'A']` | Move disk 1 from C to A |
|
| 105 |
+
| 1 | `[[1], [], [2]]` | `[[1], [2], []]` | `[2, 'C', 'B']` | Move disk 2 from C to B |
|
| 106 |
+
| 2 | `[[1], [2], []]` | `[[], [1, 2], []]` | `[1, 'A', 'B']` | Move disk 1 from A to B |
|
| 107 |
+
| 3 | `[[], [1, 2], []]` | `[[], [1, 2], []]` | `['_', '_', '_']` | Goal reached! |
|
| 108 |
+
|
| 109 |
+
### Recursive Pattern
|
| 110 |
+
|
| 111 |
+
The recursive pattern for N disks:
|
| 112 |
+
```
|
| 113 |
+
function HANOI(n, source, target, auxiliary):
|
| 114 |
+
if n == 1:
|
| 115 |
+
move disk 1 from source to target
|
| 116 |
+
else:
|
| 117 |
+
HANOI(n-1, source, auxiliary, target) # Move n-1 to aux
|
| 118 |
+
move disk n from source to target # Move largest
|
| 119 |
+
HANOI(n-1, auxiliary, target, source) # Move n-1 to target
|
| 120 |
+
```
|
| 121 |
+
|
| 122 |
+
---
|
| 123 |
+
|
| 124 |
+
## 📁 CSV Column Descriptions
|
| 125 |
+
|
| 126 |
+
### Columns
|
| 127 |
+
|
| 128 |
+
| Column | Type | Description |
|
| 129 |
+
|--------|------|-------------|
|
| 130 |
+
| `N` | int | Number of disks (difficulty parameter) |
|
| 131 |
+
| `start_state` | string | Initial configuration of all three pegs |
|
| 132 |
+
| `goal_state` | string | Target configuration to achieve |
|
| 133 |
+
| `start_peg` | string | Starting peg ('A', 'B', or 'C') |
|
| 134 |
+
| `goal_peg` | string | Target peg ('A', 'B', or 'C') |
|
| 135 |
+
| `current_state` | string | State before this move |
|
| 136 |
+
| `next_state` | string | State after applying this move |
|
| 137 |
+
| `move` | string | Action taken: `[disk, source_peg, dest_peg]` |
|
| 138 |
+
| `num_moves` | int | Total moves in optimal solution (2^N - 1) |
|
| 139 |
+
|
| 140 |
+
### Data Format
|
| 141 |
+
|
| 142 |
+
Each row represents one **move** in a solution trajectory.
|
| 143 |
+
|
| 144 |
+
**Example CSV rows:**
|
| 145 |
+
```csv
|
| 146 |
+
N,start_state,goal_state,start_peg,goal_peg,current_state,next_state,move,num_moves
|
| 147 |
+
2,"[[],[],[1,2]]","[[],[1,2],[]]",C,B,"[[],[],[1,2]]","[[1],[],[2]]","[1,'C','A']",3
|
| 148 |
+
2,"[[],[],[1,2]]","[[],[1,2],[]]",C,B,"[[1],[],[2]]","[[1],[2],[]]","[2,'C','B']",3
|
| 149 |
+
2,"[[],[],[1,2]]","[[],[1,2],[]]",C,B,"[[1],[2],[]]","[[],[1,2],[]]","[1,'A','B']",3
|
| 150 |
+
2,"[[],[],[1,2]]","[[],[1,2],[]]",C,B,"[[],[1,2],[]]","[[],[1,2],[]]","['_','_','_']",3
|
| 151 |
+
```
|
| 152 |
+
|
| 153 |
+
---
|
| 154 |
+
|
| 155 |
+
## 💡 Usage Tips
|
| 156 |
+
|
| 157 |
+
### For Model Training
|
| 158 |
+
|
| 159 |
+
⚠️ **Warning**: Tower of Hanoi is **difficult** for current sequence models.
|
| 160 |
+
|
| 161 |
+
Suggested approaches:
|
| 162 |
+
1. **Add explicit subgoal markers**: Annotate when recursive subproblems start/end
|
| 163 |
+
2. **Hierarchical representations**: Encode recursive structure explicitly
|
| 164 |
+
3. **Search augmentation**: Use beam search or MCTS during decoding
|
| 165 |
+
4. **Curriculum learning**: Start with N=1, slowly increase (but likely still fails at N≥3)
|
| 166 |
+
|
| 167 |
+
### For Evaluation
|
| 168 |
+
|
| 169 |
+
```python
|
| 170 |
+
from datasets import load_dataset
|
| 171 |
+
|
| 172 |
+
# Load Tower of Hanoi
|
| 173 |
+
dataset = load_dataset("gmannem/RecurrReason", "tower_of_hanoi")
|
| 174 |
+
|
| 175 |
+
# WARNING: Expect very low success rates!
|
| 176 |
+
# Models typically solve only N=1
|
| 177 |
+
|
| 178 |
+
def evaluate_hanoi(model, example):
|
| 179 |
+
"""
|
| 180 |
+
Evaluation with strict constraints.
|
| 181 |
+
|
| 182 |
+
A single size-ordering violation = immediate failure.
|
| 183 |
+
"""
|
| 184 |
+
current = example['start_state']
|
| 185 |
+
goal = example['goal_state']
|
| 186 |
+
steps = 0
|
| 187 |
+
max_steps = 2 * example['num_moves'] # 2 × (2^N - 1)
|
| 188 |
+
|
| 189 |
+
while steps < max_steps:
|
| 190 |
+
next_state = model.predict(current, goal)
|
| 191 |
+
|
| 192 |
+
# Check size ordering (CRITICAL!)
|
| 193 |
+
if violates_size_constraint(next_state):
|
| 194 |
+
return "INVALID_MOVE", steps
|
| 195 |
+
|
| 196 |
+
if next_state == goal:
|
| 197 |
+
return "SUCCESS", steps
|
| 198 |
+
|
| 199 |
+
current = next_state
|
| 200 |
+
steps += 1
|
| 201 |
+
|
| 202 |
+
return "TIMEOUT", steps
|
| 203 |
+
|
| 204 |
+
def violates_size_constraint(state):
|
| 205 |
+
"""Check if any peg has larger disk on top of smaller."""
|
| 206 |
+
for peg in state:
|
| 207 |
+
for i in range(len(peg) - 1):
|
| 208 |
+
if peg[i] > peg[i+1]: # Larger disk on top!
|
| 209 |
+
return True
|
| 210 |
+
return False
|
| 211 |
+
```
|
| 212 |
+
|
| 213 |
+
---
|
| 214 |
+
|
| 215 |
+
## 🔬 Research Directions
|
| 216 |
+
|
| 217 |
+
Tower of Hanoi poses fundamental challenges for sequence models:
|
| 218 |
+
|
| 219 |
+
1. **Hierarchical Planning**: How to encode recursive subgoals?
|
| 220 |
+
|
| 221 |
+
2. **Search Integration**: Can we augment models with A* or MCTS?
|
| 222 |
+
|
| 223 |
+
3. **Neuro-Symbolic Approaches**: Combine neural prediction with symbolic constraint checking
|
| 224 |
+
|
| 225 |
+
4. **Explicit Memory**: External memory to track subproblem state
|
| 226 |
+
|
| 227 |
+
5. **Length Generalization**: Current models cannot extrapolate from short to long sequences on this task
|
| 228 |
+
|
| 229 |
+
**Key Insight**: Success on Tower of Hanoi likely requires **search** or **explicit hierarchical representations**, not just larger models.
|
| 230 |
+
|
| 231 |
+
---
|
| 232 |
+
|
| 233 |
+
|
| 234 |
+
## 📚 References
|
| 235 |
+
|
| 236 |
+
**Main Paper:**
|
| 237 |
+
```bibtex
|
| 238 |
+
@inproceedings{mannem2026recurrent,
|
| 239 |
+
title={Recurrent Reasoning on Symbolic Puzzles with Sequence Models},
|
| 240 |
+
author={Gowrav Mannem and Chowdhury Marzia Mahjabin and Jason Chen and Shivank Garg and Kevin Zhu},
|
| 241 |
+
booktitle={ICLR 2026 Workshop on Logical Reasoning of Large Language Models},
|
| 242 |
+
year={2026}
|
| 243 |
+
}
|
| 244 |
+
```
|
| 245 |
+
|
| 246 |
+
**Classic Reference:**
|
| 247 |
+
```bibtex
|
| 248 |
+
@article{lucas1883tower,
|
| 249 |
+
title={Récréations mathématiques},
|
| 250 |
+
author={Lucas, Édouard},
|
| 251 |
+
journal={Gauthier-Villars},
|
| 252 |
+
year={1883}
|
| 253 |
+
}
|
| 254 |
+
```
|
| 255 |
+
|
| 256 |
+
---
|
| 257 |
+
|
| 258 |
+
[← Back to Main README](README.md)
|