Enigmatic Code

Programming Enigma Puzzles

Enigma 1480: Getting older

From New Scientist #2642, 9th February 2008

The five children Ann, Ben, Con, Den and Ern are of different ages. Three of them are honest but the other two always lie.

Ben said to Ern “I am younger than you”, and in fact there are exactly 12 choices of X and Y such that X could say that to Y.

Ten of the 12 are:

Ben to Ern,
Con to Ann,
Con to Ben,
Con to Den,
Con to Ern,
Den to Ben,
Den to Con,
Den to Ern,
Ern to Ann,
Ern to Den.

List the five children in increasing order of age.

[enigma1480]

Advertisements

One response to “Enigma 1480: Getting older

  1. Jim Randell 28 November 2012 at 7:53 am

    The following Python program runs in 51ms.

    from itertools import permutations, combinations
    from enigma import irange, printf
    
    # check a statement
    def check(liars, X, Y):
      return (X < Y) ^ (X in liars)
    
    # assign representative ages to the children
    for (A, B, C, D, E) in permutations(range(5)):
      # nominate two liars
      for liars in combinations(range(5), 2):
    
        # find the statements that could be made
        s = set((X, Y) for (X, Y) in permutations(range(5), 2) if check(liars, X, Y))
        # there should be 12 of them
        if len(s) != 12: continue
    
        # and should contain the specified pairs
        if not s.issuperset(((B, E), (C, B), (C, E), (D, C), (E, A),
                             (C, A), (C, D), (D, B), (D, E), (E, D))): continue
    
        # generate the order
        r = list(x[1] for x in sorted(zip((A, B, C, D, E), 'ABCDE')))
        printf("order = {r} [liars = {t}]", r=' '.join(r), t=' '.join(r[x] for x in liars))
    

    Solution: The list of children in increasing order of age is: Con, Em, Ben, Den, Ann.

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: