Enigmatic Code

Programming Enigma Puzzles

Puzzle 48: Verse on the island

From New Scientist #1099, 20th April 1978 [link]

We live, we three, on the Imperfect Isle,
Where all is not just what it ought to be.
One is a Wotta-Woppa and he never
Tells what is true, in fact a liar he.

And then there is another one who cannot
Make up his mind. Oh, shall I tell a lie?
He is a Shilli-Shalla, and makes statements,
One true, one false. But which? The constant cry.
The third one is a Pukka and we find
Nothing but truth comes from the third man’s mind.

Single figures all our dwellings,
And each one is different.
Three statements each, so read with care
And use your loaf to find what’s meant.

A:

(1) First let me say no Shilli-Shalla I,
But I’m afraid I cannot tell you why!
(2) Then I point out that where numbers are concerned
The lower the truer; that’s the fact for which you yearned.
(3) Thirdly, no tricks,
My number’s less than six.

B:

(1) and (2) A Pukka, I, and live at number one.
That’s two statements in a single line.
(3) Perfect, you might say, but not as perfect as C‘s square.

C:

(1) A and B live on either side of me.
(2) Who is the Wotta-Woppa? Why it’s B.
(3) And now our verse
Has done its worst.
Just to finish with a wink,
To get this right you’ll have to think.
And with a nod,
A‘s number is not odd.

Where do AB and C live and what are their tribes?

[puzzle48]

One response to “Puzzle 48: Verse on the island

  1. Jim Randell 30 May 2018 at 8:18 am

    Solving this puzzle comes down to interpreting C’s statement (1) that “A and B live on either side of me”.

    I originally took it to mean “A is my next door neighbour on one side, and B is my next door neighbour on the other side” (treated as a single statement). And assuming the houses are in a straight line numbered sequentially from 1 to 9 (or even from 0 to 9), I get 38 possible solutions (although it does include the published solution).

    But taking the statement to mean: “A lives on one side of me (but not necessarily next door) and B lives on the other side of me (but not necessarily next door)” gives only one answer, which is the published solution, so this is probably what the setter had in mind.

    I also had a bit of doubt over to the interpretation of B’s statement (3) “Perfect, you might say, but not as perfect as C‘s square”, but settled on: “C’s house number is a perfect square”.

    There is also the issue of who is narrating the poem – it implies that it is one of the tribesmen themselves. I assumed that all the text of the poem before we get to statements is true, so it must be the Pukka narrating it. But each of A, B and C is referred to in the third person in the text.

    This Python program runs in 98ms. (Note that the use of parameter unpacking on line 31 means this won’t execute directly under Python 3, but the [[ unpack() ]] function from the enigma.py library can be used to work around this issue, as I have done in the code posted on repl.it).

    Run: [ @repl.it ]

    from itertools import permutations
    from enigma import tuples, irange, is_square, printf
    
    # Pukka - always tells the truth
    def P(ss):
      return all(ss)
    
    # Wotta-Woppa - never tells the truth
    def W(ss):
      return not any(ss)
    
    # Shilli-Shalla - alternates between true and false
    def S(ss):
      return all(a ^ b for (a, b) in tuples(ss, 2))
    
    
    # possible house numbers
    numbers = set(irange(0, 9))
    
    # which of those are perfect squares?
    squares = set(n for n in numbers if is_square(n) is not None)
    
    
    # choose the tribes
    for (tA, tB, tC) in permutations((P, W, S)):
    
      # choose the numbers
      for (nA, nB, nC) in permutations(numbers, 3):
    
        # order the tribes by the house numbers
        ts = tuple(t for (n, t) in sorted([(nA, tA), (nB, tB), (nC, tC)], key=lambda (n, t): n))
    
        # A's statements
        # (1) "A is not a Shilli-Shalla"
        # (2) "the house numbers in ascending order belong to (P, S, W)"
        # (3) "A's number is less than 6
        if not tA([tA != S, ts == (P, S, W), nA < 6]): continue
    
        # B's statements:
        # (1) "B is a Pukka"
        # (2) "B's number is 1"
        # (30 "C's number is a perfect square"
        if not tB([tB == P, nB == 1, nC in squares]): continue
    
        # C's statements:
        # (1) "A and B live on opposite sides of C"
        # (2) "B is a Wotta-Woppa"
        # (3) "A's number is not odd"
        # if A and B live on opposite sides of C, then the differences in
        # their numbers have different signs, so their product is negative
        # (for next door neighbours we can check the product is -1)
        if not tC([(nC - nA) * (nC - nB) < 0, tB == W, nA % 2 != 1]): continue
    
        printf("A={tA.__name__}:{nA}, B={tB.__name__}:{nB}, C={tC.__name__}:{nC}")
    

    Solution: A is a Shilli-Shalla, and lives at number 7. B is a Pukka, and lives at number 1. C is a Wotta-Woppa, and lives at number 9.

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: