One of my favorite memes is the 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.
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.
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, so shortest answers in bytes wins!