Enigmatic Code

Programming Enigma Puzzles

Enigma 1514: Six a-leaping

From New Scientist #2676, 4th October 2008

My family has developed a board game with spaces numbered 0 to 99. A player starts with his counter at 0, throws a die and moves forward the number of spaces shown. If he lands on a space labelled with a square number he jumps forward to the next higher square (for example, if he lands on 49 he jumps to 64). If he lands on a prime number he moves back to the preceding prime number (for example, from 19 he returns to 17), except that from 2 he returns to 0.

If a player throws all six different numbers in turn:

(a) what is the maximum number on the space he can reach?
(b) what is the minimum number on the space that can be reached.



One response to “Enigma 1514: Six a-leaping

  1. Jim Randell 25 September 2012 at 8:08 am

    The following Python code runs in 43ms.

    from itertools import permutations
    from functools import reduce
    from enigma import irange, Primes, printf
    # make a dictionary of special positions
    move = dict()
    # a square number moves you to the next square number
    for i in irange(0, 8):
      move[pow(i, 2)] = pow(i + 1, 2)
    # and a prime moves you to the previous prime (but 2 -> 0)
    prev = 0
    for p in Primes(100):
      move[p] = prev
      prev = p
    # starting from sq where do you end up if you throw d?
    def game(sq, d):
      p = sq + d
      return move[p] if p in move else p
    # find the min and max positions for sequences of all possible die throws
    (maxp, minp) = (0, 99)
    for s in permutations(irange(1, 6)):
      p = reduce(game, s, 0)
      printf("{s} => {p}")
      if p > maxp: maxp = p
      if p < minp: minp = p
    printf("max = {maxp}, min = {minp}")

    Solution: The maximum number is 42. The minimum is 7.

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: