Sunday Times Teaser 2590 – Moving Downwards
by Graham Smith
Published: 13 May 2012 (link)
A three-by-three array of the nine digits 1 — 9 is said to be “downward moving” if each digit is less than the digit to the east of it and to the south of it — see, for example, the keypad on a mobile phone.
If you knew the sum of the digits in the left hand column and whether the middle digit was even or odd, you could work out my arrangement.
What is it?
One Comment
Leave one →
-
Brian Gladman permalink123456789101112131415161718192021222324252627282930313233343536373839from collections import defaultdictfrom itertools import permutations# place digits into a sequence a 3 x 3 grid# while ensuring that digits increase right# to left and top to bottomdef solve(g):# is the grid completeln = len(g)if len(g) == 9:yield gelse:# the row and column positions in the gridr, c = divmod(ln, 3)# for digits not already presentfor n in set(range(1, 10)).difference(g):# check that the digit is more than that# on its left and that above it (when# these exist)if (not ln or (not c or n > g[-1])and (not r or n > g[-3])):yield from solve(g + [n])# keep solutions in a dictionary according# to the sum of the left column and the# even/odd state of the middle elementd = {}for g in solve([]):key = (g[0] + g[3] + g[6], g[4] % 2 == 0)# if there is already a solution for this key, we cannot# tell which is a solution so we can eliminate them bothd[key] = False if key in d else gfor k, v in d.items():if k[1] and v:fs = 'The grid is:n {} {} {}n {} {} {}n {} {} {}n'print(fs.format(*v))