Sunday Times Teaser 2671 – On the Face of It
by Robin Naylor
At the start of last year my wife and I were each given clocks which ran fast by a whole number of minutes per day with my wife’s running faster than my own.
When we first looked at the clock hands they showed the same time and it turned out that this would happen at least once more during the year.
It also turned out that my clock would be right at least once during every month in the year but this was not true for my wife’s clock.
How many minutes did each clock gain in a day?
One Comment
Leave one →
-
brian gladman permalink1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556from itertools import countfrom collections import defaultdict# days in the months of a leap yearmonth_days = (0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)# find the days in a year when a clock running fast will be correct# again given the first correct day and the minutes per day gained;# then return how many times this occurs in each month in the year.def days(first_day, gain):# a count of the clock rollovers in each monthin_month = [0] * 12for mult in count(0):# the next rolloverrollover = first_day + int(mult * (720 / gain))# while it is in the yearif rollover >= 366:break# find which month it is in and add to this month's countfor m in range(12):rollover -= month_days[m + 1]if rollover < 0:in_month[m] += 1breakreturn in_month# gains for start dates in January that have and don't have# 'rollovers' in each month of the year respectivelyme, wife = defaultdict(list), defaultdict(list)for first_day in range(31):# for minutes gained per day (limits set by the need to# have at least one rollover in two months but not one# for certain in February whatever the starting date)for gain in range(12, 26):# find the counts of 'rollovers' in each monthcounts = days(first_day, gain)if 0 in counts:# my wife's clock doesn't have 'rollovers' in every monthwife[first_day].append(gain)else:# but mine doesme[first_day].append(gain)fs = 'The clocks gain {:d} and {:d} minutes per day.'# for a shared start date in Januaryfor d in set(me.keys() & wife.keys()):# the gains for wife's and my own clockfor wc_gain in wife[d]:for mc_gain in me[d]:# to have the two clocks agreeing again in a year# the wife's clock must gain two or more minutes# per day more then mineif wc_gain >= mc_gain + 2:print(fs.format(mc_gain, wc_gain))