Enigmatic Code

Programming Enigma Puzzles

Enigma 1637: In-laws

From New Scientist #2803, 12th March 2011 [link]

Four pairs of siblings were great friends right through school and eventually the eight (all mentioned below) paired off in man-woman partnerships and got married. They had no other siblings.

Alan has two brothers-in-law, Brian Long and Eleanor Short’s husband. Fiona has no sisters-in-law. Mrs Tall’s brother Colin states that his sister-in-law’s sister’s sister-in-law is Geraldine. Hannah’s sister-in-law’s brother’s sister-in-law is married to David.

Name the four married couples (eg: you might have Alan and Eleanor Long, and so on).

[enigma1637]

Advertisements

One response to “Enigma 1637: In-laws

  1. jimrandell 12 December 2011 at 4:50 pm

    The following Python program runs in 144ms. The code to select the pairs of siblings is a bit messy, but I couldn’t find a Python builtin or standard module to do it, and it’s considerably faster than a generic version I wrote.

    from itertools import permutations
    
    Male = ('Alan', 'Brian', 'Colin', 'David')
    Female = ('Eleanor', 'Fiona', 'Geraldine', 'Hannah')
    
    # make the marriage pairs
    for brides in permutations(Female, 4):
    
      # set up the married relationship
      married = {}
      for i, husband in enumerate(Male):
        married[husband] = brides[i]
        married[brides[i]] = husband
    
      # set up the siblings
      s1 = list(Male + Female)
      s1a = s1.pop(0)
      for s1b in s1:
        s2 = sorted(s1)
        s2.remove(s1b)
        s2a = s2.pop(0)
        for s2b in s2:
          s3 = sorted(s2)
          s3.remove(s2b)
          s3a = s3.pop(0)
          for s3b in s3:
            s4 = sorted(s3)
            s4.remove(s3b)
            (s4a, s4b) = s4
            sibs = ((s1a, s1b), (s2a, s2b), (s3a, s3b), (s4a, s4b))
            # and make the sibling relationship
            sibling = {s1a: s1b, s2a: s2b, s3a: s3b, s4a: s4b,
                       s1b: s1a, s2b: s2a, s3b: s3a, s4b: s4a}
    
            # set up the surnames (birthnames)
            for surnames in permutations(('Long', 'Short', 'Tall', '?')):
    
              surname = {s1a: surnames[0], s1b: surnames[0],
                         s2a: surnames[1], s2b: surnames[1],
                         s3a: surnames[2], s3b: surnames[2],
                         s4a: surnames[3], s4b: surnames[3]}
    
              # now check the statements
    
              # Statement1:
              # "Alan has two brothers-in-law, Brian Long and Eleanor Short's husband."
    
              # so Alan's brothers-in-law are his wife's brother...
              b1 = sibling[married['Alan']]
              if not(b1 in Male): continue
              # ... and his sister's husband...
              if not(sibling['Alan'] in Female): continue
              b2 = married[sibling['Alan']]
              # ... so they should be distinct...
              if b1 == b2: continue
              # ... one of them should be Brian Long, and the other Eleanor Short's husband
              if not(surname['Brian'] == 'Long'): continue
              if b1 == 'Brian':
                pass
              elif b2 == 'Brian':
                b1, b2 = b2, b1
              else:
                continue
              if not(married['Eleanor'] == b2): continue
              if not(surname[b2] == 'Short'): continue
    
              # Statement 2:
              # "Fiona has no sisters-in-law."
    
              # so Fiona's husband, should have a brother
              if not(sibling[married['Fiona']] in Male): continue
              # and Fiona's sibling must be a sister
              if not(sibling['Fiona'] in Female): continue
    
              # Statement 3:
              # "Mrs Tall's brother Colin states that his sister-in-law's sister's sister-in-law is Geraldine"
    
              # so Colin must have a sister...
              if not(sibling['Colin'] in Female): continue
              # who's married name is Tall
              if not(surname[married[sibling['Colin']]] == 'Tall'): continue
              # Colin should have a sister-in-law...
              s = [x for x in (sibling[married['Colin']], married[sibling['Colin']]) if x in Female]
              if not(len(s) == 1): continue
              # ... and they should have a sister...
              s = sibling[s[0]]
              if not(s in Female): continue
              # ... and they should have a sister-in-law
              s = [x for x in (sibling[married[s]], married[sibling[s]]) if x in Female]
              if not(len(s) == 1): continue
              if not(s[0] == 'Geraldine'): continue
    
              # Statement 4:
              # "Hannah's sister-in-law's brother's sister-in-law is married to David."
              # (This actually seems to be redundant, although it does give us the name, Hannah)
    
              # so Hannah has a sister-in-law...
              s = [x for x in (sibling[married['Hannah']], married[sibling['Hannah']]) if x in Female]
              if not(len(s) == 1): continue
              # ... who has a brother...
              s = sibling[s[0]]
              if not(s in Male): continue
              # ... who has a sister-in-law...
              s = [x for x in (sibling[married[s]], married[sibling[s]]) if x in Female]
              if not(len(s) == 1): continue
              # ... who is married to David
              if not(married[s[0]] == 'David'): continue
    
              print("Siblings:")
              print("{s}: {a} & {b}".format(s=surnames[0], a=s1a, b=s1b))
              print("{s}: {a} & {b}".format(s=surnames[1], a=s2a, b=s2b))
              print("{s}: {a} & {b}".format(s=surnames[2], a=s3a, b=s3b))
              print("{s}: {a} & {b}".format(s=surnames[3], a=s4a, b=s4b))
              print("")
    
              print("Marriages:")
              for i in Male:
                print("{a} {s} m. {b} ({n})".format(a=i, s=surname[i], b=married[i], n=surname[married[i]]))
              print("")
    

    Solution: The married couples are: Alan & Geraldine Tall, Brian & Hannah Long, Colin & Eleanor Short and David & Fiona Long.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: