I have a problem where I have to find the distance, or ticks, from one location on a lock to the other. For example, assuming a combination lock with numbers 0 – 39, I want to move from 38 to 10, and get 12, which is the number of ticks it takes to get from 38 to 10. This can be set up as (38 + x) mod 40 = 10, except that there isn’t any way to isolate the x because of the modulus operation. Can anyone think of a way to create a simple formula to find that there are 12 ticks between 38 and 10. I know how to do it using conditional branching, but I would prefer to use a formula if possible.
Share
There are two cases.
You need to calculate both and select the minimum.
Update: The previous version did not work in every case (e.g. from 1 to 25). The current version always returns the cheapest amount of ticks but without a direction (unsigned).