This is actually a previous question from a coding competition.
https://drive.google.com/file/d/1E3z--mjOODyKV6ORvKqAmf7gZ8FQDbEb/view?usp=sharing
Here's a summary of the question.
Basically, there's a grid which has a height(h) and width(w). The screensaver logo starts at (1,1) and moves one square up and one square right per second unless it hits either a border or a corner where it is reflected.
The objective is to determine the location of the screensaver logo for different grid sizes, i.e. height(h) and width(w) and at different time(t).
This is solved by using mod. But what I struggle to understand is the description below in the sample solution.
For the horizontal component, note that the column of the logo’s location repeats every 2(W-1) seconds. Therefore, we can use the value of T in modulo 2(W-1) instead (i.e. the remainder when T is divided by 2(W-1) seconds).
Question
- How can we really know that the logo's location does repeat every 2(W-1) seconds?
If the logo is in column $\ 1\ $ at time $\ t=0\ $, then it will be in column $\ 1+\tau\ $ at time $\ t=\tau\ $ as long as $\ \tau\le W-1\ $. At time $\ t=W-1\ $ it will be in column $\ W\ $ and reverse its direction of travel. Thereafter, it will be in column $\ W-\big(\tau-(W-1)\big)=2W-1-\tau\ $ at time $\ t=\tau\ $ as long as $\ \tau\le2W-2\ $. At time $\ t=2W-2\ $ it will be back in column $1$, exactly where it was at time $\ t=0\ $. Therefore, the horizontal motion repeats with period $\ 2W-2\ $.