Enigmatic Code

Programming Enigma Puzzles

Enigma 1211: Four rooms

From New Scientist #2367, 2nd November 2002 [link]

There are four logicians, Anna, Barbara, Cara and Dora, who are in four rooms, P, Q, R and S, respectively. Each room is painted yellow or green. The distribution of paints is one of the following nine: YYGY (i.e. P is Y, Q is Y, R is G and S is Y), YYYG, YYYY, YYGG, YGGY, GYYY, GGYY, GGYG and GGGG. I give the list to the four logicians. Two of them, whenever they write anything down, always write the opposite of what they believe is true. Each logician has no outside knowledge of what is happening outside her room.

Anna gives me a piece of paper on which she has written, ‘the colour of room P is …’, with a colour in place of the three dots. I give the paper to Barbara who takes the information on it as being true. She deduces, from all the information she has, what she believes to be the colour of room S; she gives me a piece of paper on which she has written, ‘the colour of room S is …’.

I give the paper to Cara, who carries out a similar activity to Barbara, ending with Cara giving me a piece of paper with a statement that room P is a certain colour. I give the paper to Dora who carries out a similar activity to Barbara and Cara, ending with Dora giving me a piece of paper with a statement that room Q is a certain colour.

I give it to Anna who carries out a similar activity to Barbara, Cara and Dora, ending with Anna giving me a piece of paper with a statement that room R is a certain colour.

What are the colours of the four rooms?

[enigma1211]

Advertisements

One response to “Enigma 1211: Four rooms

  1. Jim Randell 11 August 2015 at 8:42 am

    This Python program considers the possibilities and uses exceptions (actually assertions) to weed out cases where certain information cannot be deduced. It runs in 31ms.

    from itertools import combinations
    from enigma import printf
    
    # we'll identify the rooms P, Q, R, S with their occupants A, B, C, D
    
    # indices for A, B, C, D
    indices = (A, B, C, D) = (0, 1, 2, 3)
    
    # possible colourings
    colours = ('YYGY', 'YYYG', 'YYYY', 'YYGG', 'YGGY', 'GYYY', 'GGYY', 'GGYG', 'GGGG')
    
    # write down colour <c> with trustworthyness <t>
    def write(c, t):
      if t:
        return c
      elif c == 'Y':
        return 'G'
      else:
        return 'Y'
    
    # deduce room index <i> from the (<j>, <v>) pairs where room index <j> has colour <v>
    # if the deduction cannot be made an AssertionError is raised
    def deduce(i, *args):
      s = set(c[i] for c in colours if all(c[j] == v for (j, v) in args))
      assert len(s) == 1
      return s.pop()
    
    # choose the two truthers
    for T in combinations(indices, 2):
    
      t = tuple(p in T for p in indices)
    
      # choose an actual colouring for the rooms
      for (cA, cB, cC, cD) in colours:
    
        # A writes down the colour of their own room
        pA = write(cA, t[A])
    
        # some of the deductions might fail
        try:
    
          # B deduces the colour of D's room, given A's and B's
          pB = deduce(D, (B, cB), (A, pA))
          pB = write(pB, t[B])
    
          # C deduces the colour of A's room, given C's and D's
          pC = deduce(A, (C, cC), (D, pB))
          # and writes it down
          pC = write(pC, t[C])
    
          # D deduces the colour of B's room, given A's and D's
          pD = deduce(B, (D, cD), (A, pC))
          # and writes it down
          pD = write(pD, t[D])
    
          # A deduces the colour of C's room, given A's and B's
          pA2 = deduce(C, (A, cA), (B, pD))
          # and writes it down
          pA2 = write(pA2, t[A])
    
        except AssertionError:
          continue
    
        # output the colours of the rooms / the truthers / and the pieces of paper
        printf("A={cA} B={cB} C={cC} D={cD} / T={T} / pA={pA} pB={pB} pC={pC} pD={pD} pA2={pA2}")
    

    Solution: The rooms are painted as follow’s: Anna’s room (P) is painted yellow; Barbara’s room (Q) is painted yellow; Cara’s room (R) is painted green; Dora’s room (S) is painted green.

    If A lies, B and C tell the truth, and D lies, we have the following sequence:

    1. A knows her room is yellow, and lies, so she writes: “A’s room is green”.

    2. B “knows” that A’s room is green, and her own room is yellow. The only sequence matching GY?? is GYYY. So B “knows” that A=G, B=Y, C=Y, D=Y. B tells the truth so she writes: “D’s room is yellow”.

    3. C “knows” that D’s room is yellow, and her own room is green. The sequences matching ??GY are YYGY, YGGY. In both cases A=Y. C tells the truth, so she writes: “A’s room is yellow”.

    4. D “knows” that A’s room is yellow, and her own room is green. The sequences matching Y??G are YYYG, YYGG. In both cases B=Y. D lies, so she writes: “B’s room is green”.

    5. A “knows” that B’s room is green, and her own room is yellow. The only sequence matching YG?? are YGGY. So A “knows” that A=Y, B=G, C=G, D=Y. A lies, so she writes: “C’s room is yellow”.

    There is a second case where A lies, B tells the truth, C lies and D tells the truth. The sequence is the same up to C’s deduction:

    3. … A=Y. C lies, so she writes: “A’s room is green”.

    4. D “knows” that A’s room is green, and her own room is green. The sequences matching G??G are GGYG, GGGG. If both cases B=G. D tells the truth, so she writes: “B’s room is green”.

    5. Same as given above.

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: