Sunday Times Teaser 2914 – Bank Statement
by John Owen
Published July 29 2018 (link)
My last bank statement was most interesting. The first line showed the opening balance in pounds and pence, then each of the following four lines showed a debit together with the resulting balance. I did not go overdrawn.
Remarkably, each of the five balances used the same five different digits once only, and the largest of the digits was less than three times the smallest. Each of the four debits also used five different digits once only, but the digits in the debits were all different from those in the balances.
What was the final balance?
One Comment
Leave one →
-
Brian Gladman permalink123456789101112131415161718192021222324252627282930313233343536373839404142434445from itertools import combinations, permutationsfrom collections import defaultdictfrom functools import reduce# find a sequence of five digit balances that all use the same five# different digits with five adjacent differences each of which use# all the remaining five digits (b2lb is a map that gives for each# balance a list of lower balances that can follow it)def bal_seq(seq, b2lb):if len(seq) == 5:yield seqelif seq[-1] in b2lb.keys():for nb in b2lb[seq[-1]]:yield from bal_seq(seq + (nb,), b2lb)# consider the lowest digit in the balances (which must# allow five digits less than three times its value)for lo in range(3, 6):# pick five digits for the balancesfor c5 in combinations(range(lo, min(3 * lo, 10)), 5):# the digits for the debitsdbt_dgts = set(range(10)).difference(c5)# compile the list of balances that use these digitsbals = [reduce(lambda x, y: 10 * x + y, s) for s in permutations(c5)]# now find pairs of balances for which their (five digit) difference# uses all five digits not present in the balances (map each balance# to a list of the lower balances that can follow it)b2lb = defaultdict(list)for a, b in combinations(sorted(bals), 2):if {int(x) for x in str(b - a)} == dbt_dgts:b2lb[b].append(a)# consider each possible starting balancefor b in b2lb.keys():# and look for a sequence of balances with debits of the correct formfor s in bal_seq((b,), b2lb):sol = f'\u00a3{s[-1] / 100:.2f}'fs = '=>'.join((f'\u00a3{a / 100:.2f}-\u00a3{(a - b) / 100:.2f}')for a, b in zip(s, s[1:]))print(f'Final Balance = {sol} ({fs}=>{sol})')