Enigmatic Code

Programming Enigma Puzzles

Enigma 6: Old maid

From New Scientist #1148, 29th March 1979 [link]

Tom, Dick and Harry have only just learnt to play Old Maid, so they are using a pack with only the aces, kings, queens and jacks. In their latest game they began as usual by removing one card, unseen and dealing out the rest. As you know doubt know, the game is played by each player in turn, having discarded any pairs, taking an unseen card from the player on his left. This process continues until only one card is left and the player who has it is the Old Maid.

Pairs were discarded, leaving nine cards still in. Then the taking process started with Tom taking an ace from Dick but failing to make a pair. Then Dick took a king from Harry and failed to make a pair. Then Harry took a queen from Tom and made a pair. Then Tom took a jack from Dick and made a pair.

What was the Old Maid card and who held it?

[enigma6]

2 responses to “Enigma 6: Old maid

  1. jimrandell 5 December 2011 at 12:25 pm

    The following Python code runs in 32ms

    from itertools import product
    from enigma import printf
    
    # so, after the cards have been dealt and pairs removed each player can only
    # have 0 or 1 of each kind of card
    
    # and each hand starts off with 5 cards (of the 15 dealt)
    # and discards 2 or 4 cards, leaving 3 or 1 cards
    
    (A, K, Q, J) = (0, 1, 2, 3)
    hands = (x for x in product((0, 1), repeat=4) if sum(x) in (1, 3))
    
    # deal the hands
    for (T, D, H) in product(hands, repeat=3):
      # there are 9 cards in total
      if sum(T) + sum(D) + sum(H) != 9: continue
      # and there must be an odd number of exactly one type of card
      cards = (sum(p[t] for p in (T, D, H)) for t in (A, K, Q, J))
      if len([x for x in cards if x % 2]) != 1: continue
    
      (T, D, H) = map(list, (T, D, H))
    
      # "Tom took an ace from Dick but failed to make a pair"
      # so Tom must not have an ace, and Dick must
      if not(not(T[A]) and D[A]): continue
      (T[A], D[A]) = (1, 0)
    
      # "Dick took a king from Harry and failed to make a pair"
      # so Dick must not have an king, and Harry must
      if not(not(D[K]) and H[K]): continue
      (D[K], H[K]) = (1, 0)
    
      # "Harry took a queen from Tom and made a pair"
      # so Harry must have a queen, and so must Tom
      if not(H[Q] and T[Q]): continue
      (H[Q], T[Q]) = (0, 0)
    
      # "Tom took a jack from Dick and made a pair"
      # so Tom must have a jack, and so must Dick
      if not(T[J] and D[J]): continue
      (T[J], D[J]) = (0, 0)
    
      printf("T={T} D={D} H={H}")
    
      # find the Old Maid
      cards = [sum(p[t] for p in (T, D, H)) for t in (A, K, Q, J)]
      card = [x for x in (A, K, Q, J) if cards[x] == 1]
      assert len(card) == 1
      card = card[0]
    
      # and the holder
      holder = ('Tom' if T[card] else 'Dick' if D[card] else 'Harry')
    
      printf("Old Maid = {card}, Holder = {holder}", card='AKQJ'[card])
    

    Solution: The Old Maid card is a Queen, currently held by Dick.

    • Hugh Casement 11 September 2014 at 3:42 pm

      The solution published in the magazine the following week added:

      “After initial pairs went out, each had an odd number of cards. Tom had QJ, Dick AJ, and Harry KQ. So each had three cards and you can prove that Tom also had a K, Dick a Q, and Harry an A.”

      Not sure about the reasoning, but it may not clash with what you say in the comment lines of the program.
      Incidentally, you have Enigma 7 and 11 word perfect.

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: