Teaser 2837: Back and forth
From The Sunday Times, 5th February 2017 [link]
George and Martha have a digital 24-hour clock that always displays the time as four digits (e.g. 2:17am displays as 0217). During one lazy weekend George noted two particular times when the four-digit display was palindromic. He calculated the number of minutes from the first of these times to the second and he discovered that the answer was a three-figure palindrome. When he reported all the details to Martha, she commented that the sum of the eleven digits in the three palindromes was also palindromic.
What were the two palindromic times?
[teaser2837]
Jim Randell 8:19 am on 7 October 2019 Permalink |
This program considers when there is a corresponding minute for each hour to make a list of palindromic times, and then looks at pairs of times from this list that satisfy the remaining conditions.
It runs in 44ms.
Run: [ @repl.it ]
from itertools import product from enigma import irange, nsplit, concat, printf # palindrome check is_palindrome = lambda s: s[::-1] == s # record palindromic times ts = list() for h in irange(0, 23): (a, b) = nsplit(h, 2) m = 10 * b + a # record time (in minutes) and displayed digits if m < 60: ts.append((h * 60 + m, (a, b, b, a))) # now consider two palindromic times (the second time could be the following day) for ((t1, ds1), (t2, ds2), t3) in product(ts, ts, (0, 1440)): # calculate the difference in minutes t = t2 + t3 - t1 # it should be a 3-digit palindrome if not(99 < t < 1000): continue ds = nsplit(t) if not is_palindrome(ds): continue # calculate the sum of the digits, it should also be a palindrome s = sum(ds1) + sum(ds2) + sum(ds) if not is_palindrome(nsplit(s)): continue # output a solution printf("{ds1} -> {ds2}{x} = {t} minutes, digit sum = {s}", ds1=concat(ds1), ds2=concat(ds2), x=(" (next day)" if t3 else ""))Solution: The two times were 1001 and 0110 (the following day).
The first time is 10:01am on one day (displayed as 1001), the second time is 1:10am the following day (displayed as 0110). The difference between these times is 15 hours and 9 minutes = 909 minutes. And the sum of the digits in 1001, 0110, 909 is 22.
LikeLike
Frits 11:08 am on 22 December 2020 Permalink |
Slow solution compared to a similar program with enigma.py’s SubstitutedExpression (resp. 600 ms and 15 ms).
LikeLike