Teaser 2473: [Valentines cards]
From The Sunday Times, 14th February 2010 [link]
Ann, Beth, Cleo and Di each sent a card to a boy she would like to be her Valentine, one of Ed, Fin, Guy and Huw. Likewise, each of the boys sent a card to his choice of Valentine, one of the girls. Ann’s Valentine’s Valentine’s Valentine was Ed; Beth’s Valentine’s Valentine’s Valentine’s Valentine was Cleo, and Fin’s Valentine’s Valentine’s Valentine’s Valentine’s Valentine’s Valentine was Guy. Only Cleo, Ed and Huw received at least as many cards as their Valentines did.
Who (in order) are Ann’s, Beth’s, Cleo’s and Di’s Valentines?
This puzzle was originally published with no title.
[teaser2473]
Jim Randell 9:34 am on 14 February 2025 Permalink |
This Python program considers all possible recipients of the cards, and then eliminates candidates where the given conditions are not met. (A simple performance improvement would be to remove recipient candidates that do not include people we know received a card (i.e. C, E, G)).
It runs in 166ms. (Internal runtime is 85ms).
from enigma import (subsets, cproduct, multiset, map2str, printf) # multiple lookups on dict <d> def nget(d, k, n): while n > 0: k = d[k] n -= 1 return k # labels for the girls and boys (gs, bs) = (tuple("ABCD"), tuple("EFGH")) # choose targets for the girls and boys for (tgs, tbs) in cproduct(subsets(xs, size=len, select='M') for xs in (bs, gs)): # combine them into a single map v = dict(zip(gs, tgs)) v.update(zip(bs, tbs)) # check the conditions: # "A's Valentine's Valentine's Valentine was E" if not (nget(v, 'A', 3) == 'E'): continue # "B's Valentine's Valentine's Valentine's Valentine was C" if not (nget(v, 'B', 4) == 'C'): continue # "F's Valentine's Valentine's Valentine's Valentine's Valentine's Valentine was G" if not (nget(v, 'F', 6) == 'G'): continue # count the targets of the cards m = multiset.from_seq(tgs, tbs) # only C, E, H received at least as many cards as their Valentines did if not all((m.count(x) >= m.count(v[x])) == (x in "CEH") for x in gs + bs): continue # output solution printf("{v}", v=map2str(v, arr="->", sep=" ", enc=""))Solution: Ann, Beth, Cleo, Di sent their cards to Huw, Ed, Guy, Ed.
The full list is:
LikeLike