Enigmatic Code

Programming Enigma Puzzles

Enigma 1660: Three dozen

From New Scientist #2826, 20th August 2011 [link]

We have been playing our family board game again, where we use a board of 100 spaces numbered 0 to 99. A player starts with his counter on 0, throws a die, and moves forward the number of spaces shown. If he lands on a space marked with a square number he moves forward to the next square number (e.g. if he lands on 49 he jumps to 64). If he lands on a prime he moves back to the preceding prime (e.g. from 19 he returns to 17), except from 2, in which case he returns to 0.

Tom and I played a game. I threw first and moved to a point three times as far up the board as he did. In fact as we continued, my “score” after each throw always remained three times his. After five throws each my score reached 36. The total of all the numbers we had thrown was also 36.

In order, what five numbers did I throw?

[enigma1660]

Advertisements

5 responses to “Enigma 1660: Three dozen

  1. jimrandell 4 December 2011 at 12:43 pm

    The following Python code runs in 31ms.

    from enigma import irange, is_prime, printf
    
    squares = [n * n for n in irange(0, 9)]
    primes = [n for n in irange(2, 99) if is_prime(n)]
    
    special = { 2: 0 }
    for i in irange(2, len(squares) - 1):
      special[squares[i - 1]] = squares[i]
    for i in irange(1, len(primes) - 1):
      special[primes[i]] = primes[i - 1]
    
    def move(n):
      return special[n] if n in special else n
    
    def play(a, b, n, ad, bd):
      if n == 5:
        if a == 36 and sum(ad) + sum(bd) == 36:
          printf("A: {ad}, B: {bd}")
      else:
        for d1 in irange(1, 6):
          p1 = move(a + d1)
          for d2 in irange(1, 6):
            p2 = move(b + d2)
            if p1 == p2 * 3:
              play(p1, p2, n + 1, ad + [d1], bd + [d2])
    
    play(0, 0, 0, [], [])
    

    Solution: The 5 numbers you threw are: 4, 6, 3, 6 and 1.

  2. David Wilkinson 12 December 2011 at 11:33 pm

    The question is not asking for what Tom threw, but for what the author threw.

    If Tom threw first 4, he would move to square 1 (as 4 is a perfect square, he moves to the next smaller perfect square). If he then throws 6, he moves to square 5 (as 7 is a prime, so he moves to the next smaller prime). But this means that the author is on square 15 (3 times Tom’s square) after just two throws, which is not possible.

  3. jimrandell 13 December 2011 at 9:14 am

    Thanks for joining in. You’re right, I labelled the throws of the dice in the solution as Tom’s instead of “mine” in the solution (now edited to correct this).

    But the rules of the game are that if you land on a perfect square you move to the next highest perfect square, not the next smallest. (However if you land on a prime you do move to the next smallest prime).

    So the progress of the games described in the puzzle is:

    1st throw: I throw 4 (2²), so I move to 9 (3²). Tom throws 5 (a prime) so moves back to 3 (the next lowest prime). I am at 9, Tom is at 3. I am three times further up the board than Tom.

    2nd throw: I throw 6, which moves me to 15. Tom throws 4, which moves him to 7 (a prime), so he moves back to 5. I am at 15, Tom is at 5. I am three times further up the board than Tom.

    3rd throw: I throw 3, which moves me to 18. Tom throws 1, which moves him to 6. I am at 18, Tom is at 6. I am three times further up the board than Tom.

    4th throw: I throw 6, which moves me up to 24. Tom throws 2, which moves him to 8. I am at 24, Tom is at 8. I am three times further up the board than Tom.

    5th throw: I throw 1, which moves me to 25 (5²), so I move up to 36 (6²). Tom throws 4, and so moves up to 12. I am at 36, Tom is at 12. I am three times further up the board than Tom.

    My score is currently 36, and the sum of my throws is 4+6+3+6+1 = 20, the sum of Tom’s throws is 5+4+1+2+4 = 16, so the sum of all the throws is 36.

  4. David Wilkinson 13 December 2011 at 9:49 pm

    Thanks, Jim.

    Yes, I also spotted my error about the squares. Thanks for that. Your posting then prompted me to take another look at what I was trying to do (I thought it would be fun to write a basic program to solve it) when I discovered not only the square mistake, but that I’d missed the mumber 3 in my list of primes!

    What language is your code in? It looks very much more compact than what I used.

    Nice to make contcat.

    • jimrandell 13 December 2011 at 10:49 pm

      I’ve been using Python for the last couple of years, before that I used Perl. See http://www.python.org/ for more information.

      Once you get used to the idea of using whitespace indents to delimit blocks (something I wasn’t too keen on initially) it’s actually quite nice and compact to code in. Plus it has a nice built-in set class that is very useful for Enigma puzzles, and the standard itertools module provides some useful functionality too.

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: