How to determine if two vehicles will be in the same place during movement?

60 Views Asked by At

I have what I hope is an interesting problem. Maybe I'm just conceptualizing it poorly in my head.

I have two ships moving on a track in discrete increments (so, spaces on a board), each ship having three properties: direction (clockwise or counter-clockwise, denoted by 1 and -1), speed (positive integer), and grid point (0-160, which wraps around but that's irrelevant here). I need to determine whether the first ship, S1, will catch S2 the next time they both move.

Simple enough usually. However, here's the tricky bit. The way these ships move is that at each possible speed, so say from 50 to 0, all ships with 50 speed move 1 and have their speed reduced by 1, to 49. Then all ships with 49 speed (including those that had 50 changed to 49) move 1 place in their direction, and their speeds all become 48 and so on and so forth.

In other words, it's not like they magically teleport their speed*direction ahead, or 1 moves at a time; multiple can move at the same time, but also a faster ship can arrive where a slower one is before they've even started moving.

What I need to determine, is if at any point S1 and S2 will be on the same grid point during the movement process. I feel like there must be an elegant mathematical answer that doesn't involve the naive solution of just simulating the movement and seeing if it happens.

Hopefully I've explained that well enough? My current attempts don't particularly work, because I'm struggling to take into account the 1-by-1 multiple ship movement. Like, if S2 is 13 spaces ahead of S1 with a speed of 5, and S1's speed is 15... S1 won't catch S2.

1

There are 1 best solutions below

2
On BEST ANSWER

I hope I understood your question correctly. But just to be sure, let me recap what I understood, so that we are on the same page:

enter image description here

You have a discrete track, numbered from $1 $ to $n$ (in your case, $n = 160$). And it wraps around (this actually is quite important because, taking the example of the drawing, it ensures that the ship $B$ won't "hit the extremity", and will keep moving). This is why I wrote $n \equiv 0$.

And, whichever ship has the highest speed will move 1 step first, then lose $1$ point of speed. (Once again, if I understood your question correctly).

So for example, let's look at the drawing above, where both ships have the same direction. To be meticulous, let's write $x_A$ the position of ship $A$, and $x_B$ the position of ship $B$. But really, all we care about is the distance between them, let's call it $d$, which is equal to $|x_B - x_A|$, by definition (in the drawing above, $A$ is before $B$, so you can drop the absolute value sign, $d = x_B - x_A$. It's only here to ensure that the distance is always positive). So for example, if $A$ is on case $10$ and $B$ is on case $33$, $d = 23$.

If $a > b$ (ie, ship $A$ goes faster), then ship $B$ will stop after $b$ steps, while ship $A$ will still have $a - b$ possible moves.

So, whether it catches up on $B$ or not depends on whether $a - b$ is bigger than $d$ or not.

  • if $a - b < d$, then $A$ will not catch up on $B$, because it can't move $d$ cases in $a-b$ steps
  • if $a-b > d$, $A$ will catch up on $B$.

I will leave you the fun of figuring out the other situations, where the ships have different directions/initial speeds :)

Note: if you just want to know whether or not the ships will collide, then in the example above it doesn't actually matter that "faster ships move first". The reasoning I described above works exactly the same in a situation where both ships move at the same time. However, the speed factor will matter if you want to figure out the time (or "number of steps") it will take the ships to collide.