Shortest Path Algorithm for Analog Clock

I have a problem with homework that does my head, and will be very helpful if someone can help me point in the right direction.

If I have two minutes on an analog clock, such as t1 (55 minutes) and t2 (7 minutes), I need to calculate the shortest number of steps between two points.

What I have come up with so far are two equations:

-t1 + t2 + 60 =
    -55 + 7 + 60 
    = 12

t1 - t2 + 60 = 
    55 - 7 + 60 
    = 108

12 is lower then 108, therefore 12 steps is the shortest distance.

It seems to be okay if I compare two results and use the lowest. However, if I chose two more points, for example, let t1 = 39 and t2 = 34 and connect them to the equation:

-t1 + t2 + 60 = -39 + 34 + 60 = 55
t1 - t2 + 60 = 39 - 34 + 60 = 35

35 is lower then 55, therefore 35 steps is the shortest distance.

However, 35 is the wrong answer. 5 steps is the shortest distance (39 - 34 = 5).

My brain is a little fried, and I know that I am missing something simple. Can anyone help?

Analog clock face

+3
2

, , 60. %. , .

+5

% , : (t1; t2) : 0 (12 ), - .

, t2 >= t1, : it t2 - t1. t1 + 60 - t2

, 60 .

0

Source: https://habr.com/ru/post/1796711/


All Articles