š Parachute Trains Puzzle
A visual simulation of the "Parachute Trains" problem - an algorithmic puzzle where two trains with identical programs must meet on an infinite railway line.
š Problem Description
A helicopter drops two trains, each on a parachute, onto a straight infinite railway line. The distance between the trains is unknown, and both trains face the same direction. When they land, each parachute detaches and stays on the track next to the train.
Each train is controlled by an identical microchip running the same program. The trains:
- Do not know their position on the line
- Do not know the distance between them
- Cannot communicate with each other
- Can only: move forward/backward one unit per time step, or wait
- Can detect when they are at a parachute location (but cannot distinguish which parachute)
Goal: Design an algorithm that guarantees the two trains will eventually collide, no matter how far apart they start.
š” Algorithm Solution
The Expanding Spiral Search
Each train executes an expanding spiral search pattern from its parachute marker:
- Round 1: Move forward 1 unit ā return to parachute ā move backward 1 unit ā return to parachute
- Round 2: Move forward 2 units ā return to parachute ā move backward 2 units ā return to parachute
- Round 3: Move forward 4 units ā return to parachute ā move backward 4 units ā return to parachute
- Round n: Move forward 2n-1 units ā return ā move backward 2n-1 units ā return
Why It Works
The exponential growth (doubling the search distance each round) ensures that eventually, the search radius will exceed the initial distance between the trains. When this happens, one train will be exploring forward while the other is exploring backward (or returning), causing them to collide.
ā” Key insight:
Since both trains execute identical programs but start at different positions, they are effectively exploring different regions of the line. The exponential expansion ensures complete coverage of all possible distances in finite time. When a train reaches the other train's parachute, it adopts that parachute as its reference point, breaking the symmetry and guaranteeing collision.
š Getting Started
Prerequisites
- Node.js (v18 or higher)
- npm, yarn, pnpm, or bun
Installation
npm installRunning the Development Server
npm run devOpen http://localhost:3000 with your browser to see the simulation.
Building for Production
npm run build
npm startš® How to Use
- Set Initial Distance: Choose how far apart the trains start (1-100 units)
- Press Play: Watch the trains execute their algorithm step by step
- Adjust Speed: Control how fast the simulation runs
- Observe: See the trains explore, return to their parachutes, and eventually collide
- Reset: Try different starting distances to verify the algorithm always works
šļø Project Structure
src/
āāā app/
ā āāā page.tsx # Main application page
ā āāā documentation/ # Documentation page
ā āāā layout.tsx # Root layout
ā āāā globals.css # Global styles
āāā components/
ā āāā Header.tsx # Navigation header
ā āāā TrainVisualization.tsx # Visual representation of trains and railway
ā āāā ControlPanel.tsx # UI controls for the simulation
āāā lib/
āāā trainAlgorithm.ts # Core algorithm logic
āāā simulation.ts # Simulation state managementš¤ Development with Cursor AI
This project was developed with significant assistance from Cursor AI as the main development assistant. Here's how Cursor AI was utilized:
Algorithm Design
- Brainstorming: Cursor helped explore different algorithmic approaches
- Verification: Discussed the correctness proof and edge cases
- Optimization: Refined the implementation for clarity and efficiency
Code Generation
- Component scaffolding: Generated initial structure for React components
- TypeScript types: Created type-safe interfaces
- Utility functions: Implemented helper functions for calculations
UI/UX Development
- Layout suggestions: Provided recommendations for responsive design
- Styling: Generated Tailwind CSS classes
- Animation: Implemented smooth transitions and effects
š ļø Technologies Used
- Next.js 16 - React framework with App Router
- React 19 - UI library
- TypeScript - Type safety
- Tailwind CSS 4 - Styling
- Biome - Linting and formatting
- Cloudflare Pages - Deployment platform
š Algorithm Complexity
For trains starting at distance D apart:
- Time complexity: O(D) steps - the trains collide when the search radius reaches D
- Space complexity: O(1) - each train only needs to track its current state
- Rounds to collision: O(log D) - exponential growth means logarithmic number of rounds
š Notes on Implementation
Original Work
This implementation was developed from first principles using reasoning and AI assistance. The algorithm was derived by analyzing the problem constraints:
- Both trains must execute identical programs
- Trains can only use local information (their parachute position)
- The solution must work for any initial distance
- The approach must guarantee eventual collision
The exponential spiral search naturally emerges as a solution that satisfies all these constraints.
š Acknowledgments
- Challenge inspired by classic distributed systems and robotics problems
- Built with Cursor AI as the primary development assistant
- Deployed on Cloudflare Pages