Enigmatic Code

Programming Enigma Puzzles

Enigma 1678: Tennis tournament

From New Scientist #2844, 24th December 2011 [link]

My tennis club held a knockout tournament, in which eight players competed. The two losers of the semi-finals played a match to decide 3rd and 4th places. Matches were decided by the best of five sets, with the match ending once a player had won three sets. Two matches had to be abandoned due to poor weather. When this happened the winner was the player who had won the most completed sets when the match was abandoned – luckily this didn’t lead to a draw on either occasion.

At the end of the tournament no two players had played the same number of sets, and all players except the winner had lost more sets than they had won.

How many sets were played overall?



One response to “Enigma 1678: Tennis tournament

  1. jimrandell 21 December 2011 at 11:11 pm

    The following Python code executes in 48ms (I started with a slightly shorter program using itertools.permutations(), but I re-wrote to unroll it into individual loops with early rejection while I was waiting for the full puzzle to be published).

    # AvB CvD EvF GvH
    #  A v C   E v G
    #    A   v   E       C   v   G
    #        A (winner)      C (3rd)
    from enigma import is_distinct
    scores = ((3, 0), (3, 1), (3, 2), (1, 0), (2, 0), (2, 1))
    for AB in scores:
      nB = sum(AB)
      for CD in scores:
        nD = sum(CD)
        if not is_distinct(nD, nB): continue
        for EF in scores:
          nF = sum(EF)
          if not is_distinct(nF, nB, nD): continue
          for GH in scores:
            nH = sum(GH)
            if not is_distinct(nH, nB, nD, nF): continue
            for AC in scores:
              for CG in scores:
                wC = sum((CD[0], AC[1], CG[0]))
                lC = sum((CD[1], AC[0], CG[1]))
                if not(lC > wC): continue
                nC = wC + lC
                if not is_distinct(nC, nH, nB, nD, nF, nH): continue
                for EG in scores:
                  wG = sum((GH[0], EG[1], CG[1]))
                  lG = sum((GH[1], EG[0], CG[0]))
                  if not(lG > wG): continue
                  nG = wG + lG
                  if not is_distinct(nG, nH, nB, nD, nF, nH, nC): continue
                  for AE in scores:
                    wE = sum((EF[0], EG[0], AE[1]))
                    lE = sum((EF[1], EG[1], AE[0]))
                    if not(lE > wE): continue
                    nE = wE + lE
                    if not is_distinct(nE, nH, nB, nD, nF, nH, nC, nG): continue
                    wA = sum((AB[0], AC[0], AE[0]))
                    lA = sum((AB[1], AC[1], AE[1]))
                    nA = wA + lA
                    if not is_distinct(nA, nH, nB, nD, nF, nH, nC, nG, nE): continue
                    s = (AB, CD, EF, GH, AC, CG, EG, AE)
                    if len(list(filter(lambda x: 3 not in x, s))) != 2: continue
                    n = sum(sum(x) for x in s)
                    print(n, s)

    Solution: There were 29 sets played overall.

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: