STEM

The classic “Two Trains” problem with interactive solution

You may have heard of the classic puzzle: Two trains start from two towns, heading towards each other. One train leaves Town A heading towards Town B at some speed while at the same time, the other train leaves Town B heading towards Town A at another speed. If the distance between the two towns is x miles, how long will it take for the two trains to meet?

Assume they’re on parallel tracks, otherwise, we would more difficult puzzles to solve! You may have already solved it in the past or forgotten how to solve it. In this post, you can interactively set the speeds of each train and the distance between them and get results — not just how long it’d take for them to meet but also what distance each train had travelled by then.

I used the age-old formula I learned in my elementary Physics class: s=vt. In fact, to solve this riddle, that’s all we need. s is the distance traveled, v is the velocity (loosely aka speed), and t is the time.

Let’s call the train departing from Town A, Train A. Similarly, let’s call the train departing from Town B, Train B. If Train A is traveling at 60 MPH and Train B is traveling at 40 MPH, at what point do they meet? And how may miles had Train A and B traveled by the meeting point? That’s what we’re after. In the following script, you can enter the speed of each train, and the distance between them and get the answer! Just click on Run on the widget below and follow the prompts for your inputs.

To visually illustrate, I animated the motion of the trains at speeds using matplotlib library in Python using 60 MPH and 40 MPH respectively for Train A, Train B speeds with a distance between them at 100 miles and this is what it looks like:

In this example, it’s easy to see that Train A (blue arrow) traveled 60 miles and Train B traveled 40 miles (from the right side: 100-60) when they meet. Given the speeds, it’s exactly 1 hour from departure time.

But how is it calculated? Can you enter different values of speeds for the trains and distance between them in the interactive widget above and get the same results as the program outputs? Your answer should match. The solution above is done in Python, but you don’t have to…it can be solved on paper, calculator, Excel, or whatever because the math part is so basic…it’s about framing the problem. Let me give you some hints.

Hints:
time when they meet (t) = distances traveled by trains (s) / speed of trains (v)
distance traveled by Train A at the time the trains meet (s) = speed of Train A (v) * time of meeting (t)
distance traveled by Train B at the time the trains meet (s) = speed of Train B (v) * time of meeting (t)

If you want the solution or the solution in Python for educational purposes, including the animation code as shown above based on your inputs, you can contact me via the Contact Page.

I hope you found this post helpful and interesting. Explore this site for more tips and articles. Be sure to also check out my Patreon site where you can find free downloads and optional fee-based code and documentation. Thanks for visiting!

Back To Top