Part of the Code Golf Advent Calendar 2022 event. See the linked meta post for details.
As a Christmas gift, you were given a toy solar system. In this toy, all the orbits are circular and happen in the same plane. Each planet starts at a fixed location, has a fixed circumference orbit, and moves at a fixed speed (all in the same direction). You want to figure out, given these variables, at what time all of the planets will align (relative to the star which they orbit).
For example, if we had three planets with rings of size 4, 8, and 12, and the planets started in positions 2, 1, and 0 respectively, it would look like this:
The challenge
You are given three lists of non-negative integers, which are each \$n>1\$ items long:
- \$R_x\$, indicating the circumference of orbit for planet \$x\$ (will not be zero)
- \$P_x\$, indicating the start position of planet \$x\$ (positions are zero-indexed; you can assume \$P_x < R_x\$ for all \$x\$)
- \$S_x\$, indicating the number of units that planet \$x\$ moves along its orbit
(You may also take these as a collection of 3-tuples \$(R_x, P_x, S_x)\$ or a permutation thereof.)
Starting from \$t=0\$, after each time step, each planet moves \$S_x\$ units around their orbit (i.e. \$P_x \leftarrow (P_x + S_x) \mod R_x\$). Your goal is to find the smallest time \$t\$ where \$P_x / R_x \$ of all the planets are the same, i.e. the smallest \$t\$ such that $$((P_1 + t * S_1) \mod R_1) / R_1 = ((P_2 + t * S_2) \mod R_2) / R_2 = \ldots = ((P_n + t * S_n) \mod R_n) / R_n$$ . You may assume that such a time exists.
Test cases
\$R\$ | \$P\$ | \$S\$ | \$t\$ |
---|---|---|---|
\$[1,1]\$ | \$[0,0]\$ | \$[0,0]\$ | \$0\$ |
\$[100,100]\$ | \$[1,0]\$ | \$[1,0]\$ | \$99\$ |
\$[4,8,12]\$ | \$[0,1,0]\$ | \$[1,5,3]\$ | \$5\$ |
Standard loopholes are forbidden. Shortest code wins.