Sunday Times Teaser 2661 – Three Clocks
by John Owen
I have three 24 hour digital clocks that each give time as ‘hhmm’. One is accurate but the other two respectively gain and lose time at the same rate which is less than a minute per day. I set the inaccurate clocks to the correct time (simultaneously) once every six months.
I noticed recently that all three clocks were displaying the same four digits in a different order and that the fast clock had no digits in their correct positions.
What was the correct time at this moment?
One Comment
Leave one →

brian gladman permalink123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051from itertools import permutations# the correct displayed hoursfor hours in range(24):# the correct displayed minutesfor minutes in range(60):# the digits displayeddigits = '{:02d}{:02d}'.format(hours, minutes)# now permute these digits and find all valid timesvalid = []for p in permutations(digits):hh, mm = int(''.join(p[:2])), int(''.join(p[2:]))if hh < 24 and mm < 60 and hh != hours and mm != minutes:valid += [p]# now consider these times in pairs for the slow and fast clocksfor fast, slow in permutations(valid, 2):# no digits in the correct and fast clocks matchif any(x == y for x, y in zip(digits, fast)):continue# find the hours and minutes on the fast and slow clockshf, mf = divmod(int(''.join(fast)), 100)hs, ms = divmod(int(''.join(slow)), 100)# now find the difference between the displayed times on# the fast and slow clocks and that on the correct clockdf = 60 * (hf  hours) + mf  minutesds = 60 * (hs  hours) + ms  minutes# a fast clock displaying behind the correct clock is a day aheaddf, of = (df + 24 * 60, 1) if df < 0 else (df, 0)# a slow clock displaying ahead of the correct clock is a day behindds, os = (ds  24 * 60, 1) if ds > 0 else (ds, 0)# these differences must be less than 183 minutesif 183 < ds < 0 and 0 < df < 183:# we now know the times displayed on the clocks but we don't# know how many full days there have been since the reset.# But the rates at which the clocks gain and lose time are# equal so we have: ds / (days + os) = df / (days + of)# which gives: days = (df * os + ds * of) / (ds + df)days, r = divmod((df * os + ds * of), ds + df)# where we need days to be an integer less than 183if not r and 0 < days < 183:# output the times on the three clocksfs = '{0:s} {2:02d}:{3:02d}(r+{1:3d})'print(fs.format('Slow', days + os, hs, ms), end=', ')print(fs.format('correct', days, hours, minutes), end=', ')print(fs.format('fast', days + of, hf, mf))