[Question]
  [
One of my favorite memes is the [bouncing DVD logo](https://knowyourmeme.com/memes/bouncing-dvd-logo). Yet silly but extremely satisfying, a DVD logo keeps bouncing on a screen and if you ever happened to watch this screensaver, you were most likely anxiously waiting for the logo to exactly hit the corner.
[![enter image description here](https://i.stack.imgur.com/Y3SkO.gif)](https://i.stack.imgur.com/Y3SkO.gif)
I know part of the fun is the waiting, but let's try to predict when the DVD logo will hit the corner of the screen.
# Task
Given the dimensions and initial coordinates of the logo and the size of the grid, calculate when the logo will hit any corner for the first time.
# Specs
* In this challenge, the logo will be represented by a rectangle and the screen by a grid. The grid will always be bigger than the logo.
* The logo's starting movement will be southeast. The logo only moves diagonally. Horizontal and vertical speeds are the same and stays the same.
* The unit of time for this challenge is represented as a movement of 1 grid square in a certain direction.
* If the logo already starts in a corner, the expected answer is 0 (the logo is already touching a corner).
* The initial coordinates of the logo represents the top-left corner of the logo.
* The starting logo position will not extend outside the grid.
* You can assume for this challenge that the logo will eventually hit a corner.
* Input is flexible, read it however you see fit for you.
* Standard loopholes are not allowed.
# Example
In the example below, the initial coordinates of the logo is `i=(1,1)`, the size of the grid is `g=(20,20)`, the dimensions of the dvd logo is `d=(10,5)`.
It took `29 units of time` to reach a corner.
[![enter image description here](https://i.stack.imgur.com/LEaQE.gif)](https://i.stack.imgur.com/LEaQE.gif)
# Test Cases
```
Format: 
i , g , d --> output
#Special cases: logo starting in the four corners
(10,15), (20,20), (10,5) --> 0
(10,0), (20,20), (10,5) --> 0
(0,0), (20,20), (10,5) --> 0
(0,15), (20,20), (10,5) --> 0
#Special cases: logo starting glued to all walls
(0,7), (30,20), (7,12) --> 161
(7,0), (30,20), (7,12) --> 16
(23,3), (30,20), (7,12) --> 69
(11,8), (30,20), (7,12) --> 104
# Other test cases
(1,1), (20,20), (10,5) --> 29
(11,8), (24,50), (7,12) --> 448
(11,8), (50,24), (7,12) --> 376
(5,8), (48,39), (31,3) --> 352
```
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so shortest answers in bytes wins!
  
[Answer]
# [Wolfram Language (Mathematica)](https://www.wolfram.com/wolframscript/), 27 bytes
```
ChineseRemainder[-#,#2-#3]&
```
[Try it online!](https://tio.run/##hY89C4MwEIZ3f8VBwOmEfBi0Q4vQP1DaURyCjTSDGayb1b@ephEdSkO34314n7vr1fjQvRpNq1x3dOeHsfqprz4y9q6HOiNIeEZEk7rLYOxYk@zUVRVp0uXWKrtMycQoMjnDCyZOkdMw@QzkjAECjUH4A6PatVqETOwUCmQ8VIvN@wNygSBikDGEMgK911MW/XSv8hxl1Ct9M/86V24wL1Ec1vXsc2Uyuzc "Wolfram Language (Mathematica) – Try It Online")
This is the least \$t\ge0\$ such that \$i+t\equiv0\pmod{g-d}\$.
[Answer]
# [Python](https://www.python.org), 59 bytes
### -1 thanks to @xnor
```
f=lambda i,I,g,G,d,D:i%(g-d)+I%(G-D)and-~f(i+1,I+1,g,G,d,D)
```
[Attempt This Online!](https://ato.pxeger.com/run?1=hZFLTsMwEIbFNqcYtapktxNk59GmRWVVqeqKBScwzaOW3DiKHRAbLsKmG7gTXIBr4DRFPKSURSzb38zn387za_Vod7o8HF4am_vJ21W-VGJ_lwqQuMEC15jiaiFHpPBTOtmMyNpfUVGm_lNO5ITjxn2nKnpSFLmuQYEsQVdZSRhdeE421rAEdWkqJS0ZgO9fw4A6kIN2HFxFW7AXFcnuhULSdlDakqqWpXXrnIxNs3cTQilFfTru8H7xMbytsq0UCrbCZGYBShcajBW1lWXRBrG7DHLd1LDVdZnVxiOcIY8pAgkYBqyduJ2YHnOxI2b99B94Rnw-aaGaLAWrQSgFD24wrW7WSsIv2wx50Nn4lHtuyXqxR4IQwx48nbtbckz6ulnkDeHGPVwNNjO2C-xakPdcLvghDCKM_wijKPnmsWuPfvNw5vLGHY4SDOfHYNzl73AceN0P_wQ)
Based on the observation that we can "unroll" the box and let the logo fly in a straight line.
[Answer]
# [C (gcc)](https://gcc.gnu.org/), ~~55~~ 54 bytes
```
f(A,B,x,y,a,b){for(x-=a,a=0;A++%x|B++%(y-b);a++);b=a;}
```
[Try it online!](https://tio.run/##jZFNasMwEIX3OsUQCFh4jC3/xEmNF8kFuui2G1mRXYNrBVmhDmnO7qpNoQUrJDAMs/j03ryRCBohpqn2trjDEU/IsaLnWmlvDEqOvIyKre8vx8@d7d4pqGjBfZ8WVcmLy/TO296jZwIEwvDlIEXLOxB8kMMTdKpRMBiuTds30PZg3iTU6qhBKN1LPRA46LY3tbdY7l/7BULtsQhZhnH0XXbOKC3CECI3GT0EPs45ne/Earqj3INRwLsOPmwbnNI5Jj/KObL4qsxWzEHmdtk56QDjBJMZudq4DsVwPdeMUpsMnu2XaDByMNd0rufIZleJb/rEKWb/fdJ0fQu1nKX/0CR35cwsmdr9N5gwTH7JLCaEXKYv "C (gcc) – Try It Online")
Saved 1 thanks to @G B
*f(A,B,x,y,a,b){* function tacking :
* A B as position
* x y as grid size
* a b as logo size
*for(x-=a,y-=b* stretches grid to remove logo space.
*A++%x|B++%y* checks if both bounds are reached using positions % new grid sizes
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), ~~11~~ 10 [bytes](https://github.com/DennisMitchell/jelly/wiki/Code-page)
Followed [att](https://codegolf.stackexchange.com/users/81203/att)'s description in their [Mathematica answer](https://codegolf.stackexchange.com/a/251711/53748).
```
_µ0+⁵ọẠʋ1#
```
A full program that accepts `g`, `d`, `i` and prints the result.
**[Try it online!](https://tio.run/##y0rNyan8/z/@0FYD7UeNWx/u7n24a8GpbkPl////axgZ6BgZaP7XMDTQMQVROoaaAA "Jelly – Try It Online")**
### How?
```
_µ0+⁵ọẠʋ1# - Main Link: grid-dimensions (g), DVD-dimensions (d)
_          - g subtract d (vectorises)
 µ         - start a new monadic chain - f(g-d)
  0        - set the left argument "Step" to 0 (right argument is g-d by default)
         # - count up and find...
        1  - ...the first truthy result of:
       ʋ   -   last four links as a dyad - f(Step, g-d):
    ⁵      -     program's third argument -> initial-state (i)
   +       -     add to Step (vectorises)
     ọ     -     multiplicity (vectorises) (number of times each Step+i value
                                            divides the respective g-d value)
      Ạ    -     all? (are both non-zero?)
           - implicit print
```
[Answer]
# [Vyxal](https://github.com/Vyxal/Vyxal), 12 bytes
```
λ‹?+??-ḊA;ṅ‹
```
[Try it online!](https://vyxal.pythonanywhere.com/#WyIiLCIiLCLOu+KAuT8rPz8t4biKQTvhuYXigLkiLCIiLCJbNSw4XVxuWzQ4LDM5XVxuWzMxLDNdIl0=) [Test suite.](https://vyxal.pythonanywhere.com/#WyJBIiwiIiwizrvigLk/Kz8/LeG4ikE74bmF4oC5IiwiIiwiWzEwLDE1XSwgWzIwLDIwXSwgWzEwLDVdXG5bMTAsMF0sIFsyMCwyMF0sIFsxMCw1XVxuWzAsMF0sIFsyMCwyMF0sIFsxMCw1XVxuWzAsMTVdLCBbMjAsMjBdLCBbMTAsNV1cblswLDddLCBbMzAsMjBdLCBbNywxMl1cbls3LDBdLCBbMzAsMjBdLCBbNywxMl1cblsyMywzXSwgWzMwLDIwXSwgWzcsMTJdXG5bMTEsOF0sIFszMCwyMF0sIFs3LDEyXVxuWzEsMV0sIFsyMCwyMF0sIFsxMCw1XVxuWzExLDhdLCBbMjQsNTBdLCBbNywxMl1cblsxMSw4XSwgWzUwLDI0XSwgWzcsMTJdXG5bNSw4XSwgWzQ4LDM5XSwgWzMxLDNdIl0=)
Similar to @att's answer.
```
λ‹?+??-ḊA;ṅ‹
λ        ;ṅ   # Find the first positive integer t>0 such that:
 ‹?+   ḊA     #  t-1+x for each x in i are both divisible by, respectively...
    ??-       #  g[0]-d[0] and g[1]-d[1].
           ‹  # Subtract one from this.
```
[Answer]
# [Ruby](https://www.ruby-lang.org/), ~~56 ...~~ 50 bytes
```
->r,c,h,w,d,v{a=-r%h-=d;a+=h while(c+a)%(w-v)>0;a}
```
[Try it online!](https://tio.run/##VcrNCoJAGIXhfVfxQQiKZ2B@05CZGwkXkyYTtAghh4iufVIjSDir9zzj4/xMg03MjegQENFjennLxiww2ze@tIFiuN4ueVf6IssjmwrHG/9OdxpOHBxyneAw7W5pAmLTvlGgBkkNw0EVhGxpT4w50rr@A/M7mw1Q1WEFZvl1DXUEKQHV0g8YmT4 "Ruby – Try It Online")
### How it works:
First, unwrap the box, and reduce the coordinates. It will take `a` steps to reach the vertical boundary, and after that it will take `h-d` steps to reach the next. So we only need to check if we hit the horizontal boundary every `h-d` steps.
[Answer]
# Java, ~~73~~ 69 bytes
```
(i,I,g,G,d,D)->{int r=0;for(;(i-r)%(d-g)+(I-r--)%(D-G)>0;);return~r;}
```
Port of [*@att*'s Mathematical answer](https://codegolf.stackexchange.com/a/251711/52210).
[Try it online.](https://tio.run/##jZLBasJAEIbvPsUQKGzIrGSTBi2LngIhh3rxKB7SJIa1uspmFYrYYx@gj9gXSXdXoS0SKYQf5v8m/04yuy6OBV1Xr125KdoWngshTwMAIXWtVkVZw8yWzoCSWBVoNXfaOM2cVk5Tn5v@88BIqwstSpiBhElHBObYYIYVpj6dnmyvmoR8tVOEE0GV/0Aq2vgByami1FQpzfxpyH2uan1Q8l3xc8dt7v7wsjG51/jjTlSwNWOTuVZCNotl4V9G1nWrCQuRJQhRiOYBUyVuwB8a9sL77E5qiCOE@MJGyKLfbGRDe1gUY9wLGcNxP0TW/43uxegRk95UQwy/gYllj2OMn8zBDOOb1bp/71r/czEuW5m/tbreDncHPdybfemNJJ6YLLxABB56QR54Sw6NNRpnZM6orFE5IzUGfH18ghfIYfnnXl0HPHff)
**Explanation:**
```
(i,I,g,G,d,D)->{      // Method with 6 integer parameters and integer return-type
  int r=0;            //  Create a result-integer `r`, starting at 0
  for(;(i-r)%(d-g)    //  Loop as long as (i-r) modulo (d-g)
       +(I-r--)%(D-G) //  and (I-r) modulo (D-G) added together
       >0;);          //  is not 0 yet
                      //  (and decrease `r` by 1 after this check with `r--`)
  return~r;}          //  After the loop, return -r-1 as result
```
[Answer]
# [PARI/GP](https://pari.math.u-bordeaux.fr), 49 bytes
```
f(i,g,d,n)=lift(chinese([Mod(-i[n++],m)|m<-g-d]))
```
[Attempt This Online!](https://ato.pxeger.com/run?1=fZHdCsIgGIbpTqQjpU9QN9mC6g66ApGQrS1hLal1EHQnnewkuqa6mrTVQZAd-ejD-_p3uTmzt6va9f312FU0v_MKW6ihhJbMG1t1uNjYdn1YY7XclZha1U4mGrbkvJ3RmpaakCH4GG2Mc80JG0QXyO1t23kch8kYFaZpcAXIEAJIKcUZcKk9CgaCBfArUgcIxCLqn4n2McjCmHxMBlwMJhvqfhiRQBJRnEMeU8Bjd3qHRAoy0id9Kv1WcjBpDsn0tSX3p9L6_eCfH3sC)
A port of [@att's Mathematica answer](https://codegolf.stackexchange.com/a/251711/9288).
---
# [PARI/GP](https://pari.math.u-bordeaux.fr), 50 bytes
```
f(g,h,d,e)=t(i,j)=if(i%(g-d)+j%(h-e),1+t(i+1,j+1))
```
[Attempt This Online!](https://ato.pxeger.com/run?1=fZFBasMwFETpTUQgIOEx6EsWdhbpRYQohsSOTAjCOIsepKtusik9U3KaSna8KFRdaTRPM5L4n9-hHf1bH263r-vUlc1ddbzHCQccxX7iHoPY-477Le_LgyiGLT-VRwEqIisIQ0FCLMnHy0cbwvmdt6x8ZWH0lynKTdpsWMdbq5wlBzYLNQu9Ojo5Ip6h1aHZEWDWWpIgE12rJJRMIjrGJZGUzKD_SLZPok6rXkkNUgupl7o_iNLQGUSEJodAuT89Q6qCyfSZmKp-I7OQqoHezVdSfJVzz-ms8_0B)
A port of [@loopy walt's Python answer](https://codegolf.stackexchange.com/a/251712/9288). This is a curried function that takes input as `f(g1,g2,d1,d2)(i1,i2)`.
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), 34 bytes
```
≔Ｅ²Ｎθ≔Ｅ²ＮηＵＭη⁻ιＮ≔⁰ζＷ⊙⁺θζ﹪κ§ηλ≦⊕ζＩζ
```
[Try it online!](https://tio.run/##fY27CsMwDEX3foVGGVxIAp06hUwZUvILbmxqU0dJ/OgjP@86tFDoUNAi6dxzBy3cMAmbUu29uRB2YsaKQ0tzDKc4npVDxjgs7Lj7T@hM5FczjaMgiZpDZyh6NL/o11RwWPN218YqwJqe2NucWLZzjk8y2gmvHOrQklSPzWlznkHu@ShaGpwaFQUl37LeGQrYCB9wzVUplVBCVWxTFnBI@5t9AQ "Charcoal – Try It Online") Link is to verbose version of code. Explanation:
```
≔Ｅ²Ｎθ
```
Input the initial coordinates of the logo.
```
≔Ｅ²Ｎη
```
Input the size of the grid.
```
ＵＭη⁻ιＮ
```
Subtract the dimensions of the logo from the size of the grid.
```
≔⁰ζ
```
Start at `0` units of time.
```
Ｗ⊙⁺θζ﹪κ§ηλ
```
Move the logo by the current time and wrap it back into the grid. Until the sides of the logo line up with the sides of the grid...
```
≦⊕ζ
```
... increment the time.
```
Ｉζ
```
Output the final time.
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E/wiki/Commands), 10 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage)
```
-U∞<.Δ+XÖP
```
Another port of [*@att*'s Mathematica answer](https://codegolf.stackexchange.com/a/251711/52210), so make sure to upvote him as well!
Inputs in the order \$d,g,i\$.
[Try it online](https://tio.run/##yy9OTMpM/f9fN/RRxzwbvXNTtCMOTwv4/z/aXMfQKJYr2thAx8gASBvomMcCAA) or [verify all test cases](https://tio.run/##yy9OTMpM/V9Waa@k8KhtkoKSfWVCWPF/3dBHHfNs9M5NidSOODwt4H@tzv/o6GgjAx0jg1idaEMDHVMIZWgaG6ujgE3GAKsE6eIwG8x1DI2AAsZQeQMdc6zi5jCD0MSNjHWMsUoYGupYQCSgVsJdoGOIosHIRMcUVQNUwhSowQRFwtgQaJlOtImFjrElSAFIPBYA).
**Explanation:**
```
-        # Decrease the first two (implicit) inputs from one another: [g₁-d₁, g₂-d₂]
 U       # Pop and store this pair in variable `X`
∞        # Push an infinite positive list: [1,2,3,...]
 <       # Decrease each by 1 to make it a non-negative list: [0,1,2,...]
  .Δ     # Find the first integer `y` in this list which is truthy for:
    +    #  Add it to the third (implicit) input: [i₁+y, i₂+y]
     X   #  Push pair `X`
      Ö  #  Check if the pairs are divisible: [(i₁+y)%(g₁-d₁)==0, (i₂+y)%(g₂-d₂)==0]
       P #  Product to check if both are truthy: ((i₁+y)%(g₁-d₁)==0)*((i₂+y)%(g₂-d₂)==0)
         # (after which the found result is output implicitly)
```
[Answer]
# [Python 3](https://docs.python.org/3/) + [SymPy](https://docs.sympy.org/latest/index.html), 127 bytes
A port of [@att's answer](https://codegolf.stackexchange.com/a/251711/110802).
---
Golfed version. [Try it online!](https://tio.run/##jZLdasMwDIXv/RSiu7GHW/LT0K7QQR9iVyEUt3ZaQ2IH2xmE0mfPlNJugW3ZdCUfPukIS00XztakfV86W4Pv6qZbmHBW1nWL2sq2Eg503VgX4OgCuVGmReqhCudEB8LDjpTbStQHKUDzE5cb5Cnd0ROb76hkbBFspX2gjFMU9EhgeVT0x7M2yqu9U7XQRiq3LUlQPuyPwisPW8gJYNA8jjjEWcEhTzBLoiEbtKxgfIREk8R/gD9M8LUa5PQTWGFN8gWs7h6/AknKIZ0k4pjDeprA5/RfPFokS9QnTbKhx/IHIrsDyzUO/HKbJx5GZ6QgGE/w5hXg0cBoYcNqQaoStypBHOy7IqXFWzJNG/ahbSqF@ahgc/NqnDaBlrPLiLvC/BUu3@6DPo8Ydp0x0n8A)
```
from sympy.ntheory.modular import crt
from numpy import array as A
f=lambda i,g,d:crt((A(g)-A(d)).tolist(),(-A(i)).tolist())[0]
```
Ungolfed version. [Try it online!](https://tio.run/##jZLJasMwEIbveoohvVhFMV5ikhbat@jJBOPGciKIJSHJBRPy7O7ISRxDW6c6zfLNgubXnTsomfZ9bVQDtmt0F0p34Mp0YaOq9lgaEI1WxsHOOHI1ZYsclBakJqTiNewOQnLLC8ObUsiKm0Aw2DOo6CsBfKIojSk7eIOl1OFgB4IOqX015sbUnsLy7lUX0HDXGunXCG41oVNHYV1A2W3CGKF5tCXEceuKXWm5xfb50CbI44hBnG0Z5AlaSeQtH8u2lE2QaJb4D/BgCHprH05HYI01yR1YX2f8CSQpg3SWiGMGm3kC3fm/uLVIVhifHZL5HqtfiOwKrDa48MuwT@xXpwSPRMgTfFgOKDuYHMzfEVBcqKwKyk/1xUmtUI1St65wrT5ytCcFF6VpI6QL6sVpwp1h@Q6nnxp9njD0vKCk778B)
```
from sympy.ntheory.modular import crt
import numpy as np
def chinese_remainder(i, g, d):
    i_array = -np.array(i)
    gd_array = np.array(g) - np.array(d)
    return crt(gd_array.tolist(), i_array.tolist())[0]
test_cases = [
    ([10, 15], [20, 20], [10, 5]),
    ([10, 0], [20, 20], [10, 5]),
    ([0, 0], [20, 20], [10, 5]),
    ([0, 15], [20, 20], [10, 5]),
    ([0, 7], [30, 20], [7, 12]),
    ([7, 0], [30, 20], [7, 12]),
    ([23, 3], [30, 20], [7, 12]),
    ([11, 8], [30, 20], [7, 12]),
    ([1, 1], [20, 20], [10, 5]),
    ([11, 8], [24, 50], [7, 12]),
    ([11, 8], [50, 24], [7, 12]),
    ([5, 8], [48, 39], [31, 3])
]
# Use the test_cases list defined above
for input_tuple in test_cases:
    print(f"{input_tuple} -> {chinese_remainder(*input_tuple)}")
```
]