Sunday Times Teaser 2504 – No Title
by Bob Walker
Published September 19 2010 (link)
Here are 18 black cards and there is a cross on the back of some of them.
The clue in any gap indicates how many crosses there are on cards that are adjacent to that gap.
How many black cards have a cross on them?
One Comment
Leave one →
-
Brian Gladman permalink123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263from operator import eq, ne, gt, ltgrid = '''+---+---+---+---+---+---+| B | | B | | B |<>0|+---+---+---+---+---+---+|==1| B | >2| B | <2| B |+---+---+---+---+---+---+| B | | B |<>1| B | |+---+---+---+---+---+---+| | B | | B | >1| B |+---+---+---+---+---+---+| B | >2| B |<>3| B | |+---+---+---+---+---+---+|<>2| B | | B |==1| B |+---+---+---+---+---+---+'''# black cell n (n in 0..17), when present in this dictionary,# indicates the conditions that can be computed when the cell# has just been filledchk = { 5:(0,), 6:(1,), 7:(2,), 8: (3,), 10:(4,),14:(5,), 15:(6, 8), 16:(7,), 17: (9,)}# for 'condition' cell n (n in 0..9), item[n] in this tuple gives the numbers# of the black cells that contribute to its valueadj = ((2, 5), (0, 3, 6), (1, 3, 4, 7), (2, 4, 5, 8), (4, 7, 8, 10),(8, 10, 11, 14), (9, 12, 13, 15), (10, 13, 14, 16), (12, 15), (14, 16, 17))# the operators and the values for the 10 condition cellsop = ((ne, 0), (eq, 1), (gt, 2), (lt, 2), (ne, 1),(gt, 1), (gt, 2), (ne, 3), (ne, 2), (eq, 1))def set_cards(g, adj, chk):ln = len(g)# can we check any condition cells?if ln - 1 in chk:# for each cell that we can checkfor cc in chk[ln - 1]:# sum its adjacent (black card) cellsv = sum(g[i] for i in adj[cc])# exit if the condition is not metif not op[cc][0](v, op[cc][1]):returnif ln == 18:yield gelse:# try the next card without and with a crossfor x in (0, 1):# continue to the next black cardyield from set_cards(g + (x,), adj, chk)for g in set_cards(tuple(), adj, chk):# output the gridss, cnt = '', 0for c in grid:if c == 'B':ss += str(g[cnt])cnt += 1else:ss += cprint(ss, ' count =', sum(g))