Enigmatic Code

Programming Enigma Puzzles

Enigma 422: Keeping fit by halves

From New Scientist #1572, 6th August 1987 [link]

In our local sports club everyone plays at least one of badminton, squash and tennis. Of those who don’t play badminton, half play squash. Of those who don’t play squash, half play badminton. Of those who play badminton and squash, half play tennis.

I play badminton only: there are two more players who play tennis only than there are who play badminton only.

If a player plays just two of the three games, then his or her spouse also plays just two of the three games.

The membership consists entirely of married couples and each of the three games is played by at least one member of each married couple.

How many people are there in the club, and how many of those play all three games?


2 responses to “Enigma 422: Keeping fit by halves

  1. Jim Randell 10 November 2017 at 9:03 am

    We start by considering the make up of the couples.

    If one half of a couple plays two of the sports, then the other half also plays two of the sports, but between them each couple must play all of the sports. So the only possible pairs that contain two of the sports are:

    (BS, BT) = C4
    (BS, ST) = C5
    (BT, ST) = C6

    For couples where one half only plays one sport, the other half has to play at least the other two sports, but cannot play exactly two sports, so must play all three sports. Giving us the following pairings:

    (B, BST) = C1
    (S, BST) = C2
    (T, BST) = C3

    The only possible remaining pairing is:

    (BST, BST) = C7

    If we can work out how many couples there are in each category C1-C7, then we know the total number of couples in the club, and hence the total membership of the club.

    And we can calculate the number of club members who play a particular combination of games as follows:

    B = C1
    S = C2
    T = C3
    BS = C4 + C5
    BT = C4 + C6
    ST = C5 + C6
    BST = C1 + C2 + C3 + 2×C7

    And then use these values to apply the additional conditions given in the puzzle text.

    This Python 3 program considers increasing numbers of couples, until it finds a breakdown that satisfies the conditions of the puzzle. It runs in 326ms.

    Run: [ @repl.it ]

    from itertools import count
    from enigma import irange, printf
    # decompose <n> into <k> non-negative integers
    def decompose(n, k, s=[]):
      if k == 1:
        yield s + [n]
        for x in irange(0, n):
          yield from decompose(n - x, k - 1, s + [x])
    # consider increasing numbers of couples
    for N in count(1):
      t = 0
      # and allocate each type of couple  
      for (C1, C2, C3, C4, C5, C6, C7) in decompose(N, 7):
        # calculate the numbers doing each combination of sports
        (B, S, T) = (C1, C2, C3)
        (BS, BT, ST) = (C4 + C5, C4 + C6, C5 + C6)
        BST = C1 + C2 + C3 + 2 * C7
        # check all the conditions hold
        if not all((T == S + ST, T == B + BT, BST == BS, B > 0, T == B + 2)): continue
        # output solutions
        printf("{N} couples [BST={BST} BS={BS} BT={BT} ST={ST} B={B} S={S} T={T}] [C1={C1} C2={C2} C3={C3} C4={C4} C5={C5} C6={C6} C7={C7}]")
        t += 1
      # are we done?
      if t > 0: break

    Solution: There are 24 people in the club, 6 of them play all three games.

    There are 12 couples, with the following numbers of each type of couple:

    C1=2, C2=0, C3=4, C4=2, C5=4, C6=0, C7=0

    And this gives us the following number of people who play each combination of sports:

    BST=6, BS=6, BT=2, ST=4, B=2, S=0, T=4

    Further analysis of the equations derived from the conditions of the puzzle shows that this is the only solution.

  2. Jim Randell 10 November 2017 at 9:12 am

    Here is the problem expressed as a set of MiniZinc constraints:

    %#! mzn-g12mip -a
    % numbers of couples of each category
    var int: C1;
    var int: C2;
    var int: C3;
    var int: C4;
    var int: C5;
    var int: C6;
    var int: C7;
    % each is non-negative
    constraint forall (x in [C1, C2, C3, C4, C5, C6, C7]) (x >= 0);
    % number of members playing each combination of sports
    var int: B = C1;
    var int: S = C2;
    var int: T = C3;
    var int: BS = C4 + C5;
    var int: BT = C4 + C6;
    var int: ST = C5 + C6;
    var int: BST = C1 + C2 + C3 + 2 * C7;
    % [1] "Of those who don't play badminton, half play squash"
    constraint T = S + ST;
    % [2] "Of those who don't play squash, half play badminton"
    constraint T = B + BT;
    % [3] "Of those who play badminton and squash, half play tennis"
    constraint BST = BS;
    % [4] "I play badminton only"
    constraint B > 0;
    % [5] "there are two more players who play tennis only than play badminton only"
    constraint T = B + 2;
    solve satisfy;

    Of the solvers I have available I found only the [[ mzn-g12mip ]] solver was able to run the model correctly in a reasonable time.

    If the assignments for the members playing each combination are instead expressed as constraints then the [[ mzn-g12fd ]] solver also works.

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: