Enigmatic Code

Programming Enigma Puzzles

Enigma 1273: Unless

From New Scientist #2431, 24th January 2004

That great artist Pussicato has painted a picture containing antelope, bears, cats and dogs. For aesthetic reasons he kept the following conditions:

Writing “a” for the number of antelopes in the picture, “b” for the number of bears and so on;

a + 6d = 2b + c + 20 unless a + 6d = 2b + c + 17;
a + d = b + 2c + 2 unless a + d = b + 2c + 1;
3a + 5d = 3b + 5c + 11 unless 3a + 5d = 3b + 5c + 12;
3b + 5c = 2a + 2d + 1 unless 3b + 5c = 2a + 2d + 3;
the total number of animals is odd unless it is divisible by 5.

How many of each animal are there in the picture?

[enigma1273]

Advertisements

2 responses to “Enigma 1273: Unless

  1. Jim Randell 5 December 2014 at 8:24 am

    It is fairly straightforward to use SymPy to consider all the possible combinations of the first four simultaneous equations, and then look for solutions that match (exactly) one of the remaining conditions.

    This Python program runs in 433ms.

    from sympy import symbols, Eq, solve
    from itertools import product
    
    # symbols
    ss = (a, b, c, d) = symbols(('a', 'b', 'c', 'd'))
    
    # equations
    eqs = (
      (Eq(a + 6 * d, 2 * b + c + 20), Eq(a + 6 * d, 2 * b + c + 17)),
      (Eq(a + d, b + 2 * c + 2), Eq(a + d, b + 2 * c + 1)),
      (Eq(3 * a + 5 * d, 3 * b + 5 * c + 11), Eq(3 * a + 5 * d, 3 * b + 5 * c + 12)),
      (Eq(3 * b + 5 * c, 2 * a + 2 * d + 1), Eq(3 * b + 5 * c, 2 * a + 2 * d + 3)),
    )
    
    # choose one equation from each pair to be true
    for s in product(*eqs):
      r = solve(s)
      # check all numbers are positive
      if not all(r[x] > 0 for x in ss): continue
      # calculate the sum
      t = sum(r[x] for x in ss)
      if (t % 2 > 0) ^ (t % 5 == 0):
        print(r)
    

    Solution: There are 3 antelope, 1 bear, 2 cats and 3 dogs in the picture.

  2. Jim Randell 5 December 2014 at 11:50 am

    If you solve the simultaneous equations manually (or use an LP solver, like I did for Enigma 1278 and Enigma 1292), you can then try the various combinations of the constants until you find one that matches the conditions.

    This Python program runs in 32ms.

    from itertools import product
    from enigma import printf
    
    # [1]: (a + 6d) - (2b + c) = k1, (k1 is 17 or 20)
    # [2]: (a + d) - (b + 2c) = k2, (k2 is 1 or 2)
    # [3]: (3a + 5d) - (3b + 5c) = k3, (k3 is 11 or 12)
    # [4]: (3b + 5c) - (2a + 2d) = k4, (k4 is 1 or 3)
    #
    # -3[1] - 21[2] + 7[3] - 2[4]: a = -3 k1 - 21 k2 + 7 k3 - 2 k4
    #  2[1] + 19[2] - 5[3] + 3[4]: b =  2 k1 + 19 k2 - 5 k3 + 3 k4
    # -2[1] - 17[2] + 5[3] - 2[4]: c = -2 k1 - 17 k2 + 5 k3 - 2 k4
    #   [1] +  7[2] - 2[3] +  [4]: d =    k1 +  7 k2 - 2 k3 +   k4
    
    # consider possibilities for k1, k2, k3, k4
    for (k1, k2, k3, k4) in product((17, 20), (1, 2), (11, 12), (1, 3)):
    
      # calculate a, b, c, d
      a = -3 * k1 - 21 * k2 + 7 * k3 - 2 * k4
      b =  2 * k1 + 19 * k2 - 5 * k3 + 3 * k4
      c = -2 * k1 - 17 * k2 + 5 * k3 - 2 * k4
      d =      k1 +  7 * k2 - 2 * k3 +     k4
      if a < 0 or b < 0 or c < 0 or d < 0: continue
    
      # and the total number of animals
      t = a + b + c + d
      if not((t % 2 > 0) ^ (t % 5 == 0)): continue
    
      printf("a={a} b={b} c={c} d={d} [t={t} k1={k1} k2={k2} k3={k3} k4={k4}]")
    

    The constants are: 17 for the first equation, 1 for the second equation, 11 for the third equation and 1 for the fourth equation.

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: