Enigmatic Code

Programming Enigma Puzzles

Puzzle 86: The worst was first

From New Scientist #1138, 18th January 1979 [link]

A lot of experts did a great deal of hard thinking to produce a new football method designed to encourage more goals and therefore to produce matches that would be likely to be more attractive to the spectators. Under this method 10 points were awarded for a win, 5 points for a draw and 1 point for each goal scored, whatever the result of the match. But it seems that perhaps there was not enough thinking, for in a recent competition between four teams, A, B, C and D, who all played each other once, the team that came first lost all their matches. The result was as follows:

B – 45 points
D – 43 points
A – 39 points
C – 34 points

In the matches between A, C and D not more than 3 goals were scored in any match, and in the matches which B played neither side scored more than 18 goals. Each match that was won was won by a single goal.

Find the score in each match.

[puzzle86]

Advertisements

One response to “Puzzle 86: The worst was first

  1. Jim Randell 21 December 2016 at 8:00 am

    This program uses the Football() helper class from the enigma.py library. It’s not particularly elegant, but it runs in 160ms.

    from itertools import product
    from enigma import Football, irange, printf
    
    # scoring system (we add in the individual points for the goals separately)
    football = Football(games='wdl', points={ 'w': 10, 'd': 5 })
    
    # in the matches between A, C, D no more than 3 goals were scored
    # and winning matches were won by one goal
    scores = {
      'd': [ (0, 0), (1, 1) ],
      'w': [ (2, 1), (1, 0) ],
      'l': [ (1, 2), (0, 1) ],
    }
    
    # B lost all their matches, so all their 45 points come from goals
    # no side scored more than 18 goals and each game was won by a single
    # goal
    
    (AB, BC, BD) = ('w', 'l', 'l')
    
    # goals for A in the AB match
    for gABA in irange(1, 18):
      gABB = gABA - 1
      # goals for C in the BC match
      for gBCC in irange(1, 18):
        gBCB = gBCC - 1
        # goals in the BD match
        gBDB = 45 - (gABB + gBCB)
        gBDD = gBDB + 1
        if gBDD > 18: continue
        (sAB, sBC, sBD) = ((gABA, gABB), (gBCB, gBCC), (gBDB, gBDD))
    
        # now consider remaining matches
        for (AC, AD, CD) in football.games(repeat=3):
          # and the scores in those matches
          for (sAC, sAD, sCD) in product(scores[AC], scores[AD], scores[CD]):
    
            # compute the points and goals for A
            A = football.table([AB, AC, AD], [0, 0, 0])
            (fA, aA) = football.goals([sAB, sAC, sAD], [0, 0, 0])
            # add in the points for the goals
            if A.points + fA != 39: continue
    
            # points and goals for C
            C = football.table([AC, BC, CD], [1, 1, 0])
            (fC, aC) = football.goals([sAC, sBC, sCD], [1, 1, 0])
            # add in the points for the goals
            if C.points + fC != 34: continue
    
            # points and goals for D
            D = football.table([AD, BD, CD], [1, 1, 1])
            (fD, aD) = football.goals([sAD, sBD, sCD], [1, 1, 1])
            # add in the points for the goals
            if D.points + fD != 43: continue
    
            # output the match outcomes and goals
            printf("AB={AB}:{sAB} AC={AC}:{sAC} AD={AD}:{sAD} BC={BC}:{sBC} BD={BD}:{sBD} CD={CD}:{sCD}")
    

    Solution: The scores in the matches are: A v B = 16-15; A v C = 2-1; A v D = 1-2; B v C = 16-17; B v D = 14-15; C v D = 1-1.

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: