Sunday Times Teaser 2579 – Box Clever
by Victor Bryant
Published: 26 February 2012 (link)
I have placed the digits 1 to 9 in a 3×3 grid made up of nine boxes. For each two-by-two array within the grid, the sum of its four entries is the same. Furthermore, if you reverse the order of the digits in that common total, then you get the sum of the four corner entries from the original grid.
Which digits are adjacent to the 6? (ie whose boxes have a common side with 6’s box?)
One Comment
Leave one →
-
Brian Gladman permalink1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950from itertools import permutations# In the following the square grid values are:## a b c# d e f# g h idef solve():# pick four numbers for the upper left sub-arrayfor p in permutations(range(1, 10), 4):a, b, d, e = psub_sum = sum(p)# now pick two numbers for the remaing upper# right sub-arrayr1 = set(range(1, 10)).difference(p)for q in permutations(r1, 2):c, f = q# check that it has the saame sumif sum((b, c, e, f)) == sub_sum:# now permute the remainder for the bottom rowfor r in permutations(r1.difference(q)):g, h, i = r# check that the lower left and right sub-array sums# are the sameif sum((d, e, g, h)) == sum((e, f, h, i)) == sub_sum:# and that the corner sum is the sub-array sum with# its digits reversedif str(sum((a, c, g, i))) == str(sub_sum)[::-1]:# now find the horizontally and vertically# values adjacent to the sixg = (a, b, c, d, e, f, g, h, i)# find the row and column position of the sixr6, c6 = divmod(g.index(6), 3)# now accumulate values above/below and left/right# of the sixvals = []for i, g in enumerate(g):r, c = divmod(i, 3)if (not r - r6 and abs(c - c6) == 1or abs(r - r6) == 1 and not c - c6):vals += [g]yield tuple(sorted(vals))print('The values adjacent to the six are {}'.format(*set(solve())))