New Scientist Enigma 497 – The Longest Puzzle in the World?
by Keith Austin
From Issue #1649, 28th January 1989
Hannah, Joan and Sarah each live at a different one of the three houses numbered l, 2, 3. Alan, Michael and Peter each painted a different one of those three houses. Each of the three women met a different one of the three men at the shops.
There are four clues as to who lives where and so on. However, as the clues are very long they are given here only in a condensed form, and so you may wish first to write them out in full, as indicated:
(1) Write out “the man who painted the house of the woman who met, at the shops,” (1988 + the number of the house of the woman met by Peter) times. Write the copies of the repeated phrase one after the other, and then, in from of the first copy, write “Alan is” and, after the last copy, write “Alan.” to make one very long sentence which is the first clue.
(2) This is similar to clue (1), but has “Michael is” at the start and “Peter.” at the end.
(3) Write out “the woman who met, at the shops, the man who painted the house of” (1066 + the number of the house painted by the man who met Hannah) times. Add “Hannah is” at the start and “Joan.” at the end.
(4) This is similar to clue (3) but the repeated phrase is “the house that was painted by the man who met, at the shops, the woman who lives at”. The clue starts with “Number 1 is” and ends with “Number 2.”
What are the facts, that is, who lives where, who painted which house, who met who?
-
Brian Gladman permalink123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051from itertools import permutations# iterate a transformation x -> w[v[u[x]]] until it repeats# and then calculate the result of applying it n timesdef repeat(x, n, u, v, w):r, s = x, [x]for i in range(n):r = w[v[u[r]]]s.append(r)if i and r == x:breakreturn s[n % (i + 1)]# permute the women's names for houses 1, 2 and 3for w in permutations('HJS'):w2n = dict(zip(w, (1, 2, 3)))n2w = dict(zip((1, 2, 3), w))# permute the painter's names for houses 1, 2 and 3for p in permutations('AMP'):m2n = dict(zip(p, (1, 2, 3)))n2m = dict(zip((1, 2, 3), p))# permute the men's names for who met Hannah, Joan and Sarahfor m in permutations('AMP'):m2w = dict(zip(m, 'HJS'))w2m = dict(zip('HJS', m))# Clue (1) backwards from end: man -> woman -> house -> manif repeat('A', 1988 + w2n[m2w['P']], m2w, w2n, n2m) != 'A':continue# Clue (2) backwards from end: man -> woman -> house -> manif repeat('P', 1988 + w2n[m2w['P']], m2w, w2n, n2m) != 'M':continue# Clue (3) backwards from end: woman -> house -> man -> womanif repeat('J', 1066 + m2n[w2m['H']], w2n, n2m, m2w) != 'H':continue# Clue (4) backwards from end: house -> woman -> man -> houseif repeat(2, 1066 + m2n[w2m['H']], n2w, w2m, m2n) != 1:continue# compile the resultst = {s[0]:s for s in 'Joan Hannah Sarah Alan Michael Peter'.split()}print(', '.join(f"{t[w]} lives in number {n}" for w, n in w2n.items()))print(', '.join(f"{t[m]} paints number {n}" for m, n in m2n.items()))print(', '.join(f"{t[w]} meets {t[m]}" for w, m in w2m.items()))