Brain-Teaser 768: Bus ticket numbers
From The Sunday Times, 4th April 1976 [link]
On a recent bus journey I purchased the tickets for my wife and myself. On each was a four-figure number, and the sum of all eight digits was twenty-five.
I remarked upon this to my wife who thereupon asked if any digit appeared more than twice in total, and whether the sum of the digits on either ticket was equal to thirteen.
I answered both questions and my wife was able to deduce the two numbers on the tickets.
What were they?
This puzzle is included in the book The Sunday Times Book of Brain-Teasers: Book 1 (1980). The puzzle text above is taken from the book.
[teaser768]
Jim Randell 9:41 am on 18 May 2021 Permalink |
If the ticket numbers are randomised then there is no solution. So I assumed that the tickets are consecutively numbered (I think the puzzle text should have stated this).
This Python program runs in 84ms.
Run: [ @replit ]
from enigma import irange, tuples, nsplit, unpack, multiset, filter_unique, nconcat, printf # consider possible consecutive values for two tickets tickets = tuples((nsplit(n, 4) for n in irange(1, 9999)), 2) # the sum of all the digits is 25 tickets = filter(unpack(lambda xs, ys: sum(xs) + sum(ys) == 25), tickets) # questions asked def q(xs, ys): # does any digit appear more than twice? ds = multiset.from_seq(xs, ys) q1 = any(v > 2 for v in ds.values()) # do the digits on either ticket sum to 13? q2 = (sum(xs) == 13 or sum(ys) == 13) return (q1, q2) # the answers to the questions allow the tickets to be deduced ss = filter_unique(tickets, unpack(q)).unique # output solutions for (xs, ys) in ss: printf("tickets = {x}, {y}", x=nconcat(xs), y=nconcat(ys))Solution: The ticket numbers were 1299 and 1300.
So the answers to questions were: No (no digit appears more than twice in total), and no (the digits on neither ticket sum to 13).
For (yes, no), there are only three pairs: 0399 and 0400; 2199 and 2200; 3099 and 3100.
For (no, yes) there are 132 pairs, and for (yes, yes) there are 273 pairs.
LikeLike
Frits 8:25 pm on 31 May 2021 Permalink |
Doing the same (but not allowing leading zeroes) without using all the handy stuff in enigma.py.
Some steps could have been combined.
# convert digits to number d2n = lambda args: int("".join(map(str, args))) # return entries where the combination of columns <col1> and <col2> is unique def unique_cols(seq, col1=0, col2=1): return [s1 for s1 in seq if len([1 for s2 in seq if s2[col1] == s1[col1] and s2[col2] == s1[col2]]) == 1] # consider possible consecutive values for two tickets tickets = list(zip((s := [[int(x) for x in str(n)] for n in range(1000, 10000)]), s[1:])) # the sum of all the digits is 25 tickets = [(x, y) for (x, y) in tickets if sum(x) + sum(y) == 25] # store answers to the 2 questions tickets = [(any((s := x + y).count(p) > 2 for p in s), sum(x) == 13 or sum(y) == 13, x, y) for (x, y) in tickets] # the answers to the questions allow the tickets to be deduced tickets = unique_cols(tickets) for t in tickets: print(f"tickets = {d2n(t[2])}, {d2n(t[3])}")LikeLike