# Enigmatic Code

Programming Enigma Puzzles

## Puzzle 17: Goals rewarded

From New Scientist #1068, 8th September 1977 [link]

A lot of people have been of the opinion for some time that in football competitions some importance should be attached to the number of goals scored as well as to the actual result of the game. It is hoped that this will lead to more goals and therefore to more attractive games.

Three local teams of my acquaintance have been experimenting on these lines. The have had a competition in which they all played each other once, and they have awarded ten points for a win, five points for a draw, no points of course for a loss, and one point for each goal scored.

As a result of this competition one team scored 16 points, the second scored 18 points, and the third scored 10 points. It was interesting to notice that at least one goal was scored by both sides in every match.

What was the score in each match?

[puzzle17]

### One response to “Puzzle 17: Goals rewarded”

1. Jim Randell 31 July 2019 at 9:16 am

This Python program works by looking at the possible match outcomes, and calculating the non-goal points awarded, from which we can determine the “goals for” for each team. It then chooses “goals against” values with the same sum, and looks for values that give viable match scores. It uses the [[ Football() ]] helper class from the enigma.py library. It runs in 102ms.

Run: [ @repl.it ]

```from enigma import Football, irange, printf

# decompose <t> into <k> numbers, minimum <m>
def decompose(t, k, m, s=[]):
if k == 1:
if not(t < m):
yield s + [t]
else:
for x in irange(m, t - k * m):
yield from decompose(t - x, k - 1, m, s + [x])

# scoring system (non-goal points)
football = Football(points=dict(w=10, d=5), games='wdl')

# find possible match outcomes
for (AB, AC, BC) in football.games(repeat=3):
# construct the table
A = football.table([AB, AC], [0, 0])
B = football.table([AB, BC], [1, 0])
C = football.table([AC, BC], [1, 1])
# calculate the goals for values
(fA, fB, fC) = (16 - A.points, 18 - B.points, 10 - C.points)
if fA < 2 or fB < 2 or fC < 2: continue

# now try possible values for goals against
for (aA, aB, aC) in decompose(fA + fB + fC, 3, 2):

# scores in A's matches
for (sAB, sAC) in football.scores([AB, AC], [0, 0], fA, aA):
if 0 in sAB or 0 in sAC: continue

# scores in the remaining match (using B's goals for/against)
for (sBC,) in football.scores([BC], [0], fB, aB, [sAB], [1]):
if 0 in sBC: continue

# and check goals for/against C
if not(football.goals([sAC, sBC], [1, 1]) == (fC, aC)): continue

# output matches and scores
printf("AvB={AB}:{sAB} AvC={AC}:{sAC} BvC={BC}:{sBC}")
```

Solution: The scores in the matches were: A vs B = 1-2; A vs C = 5-4; B vs C = 1-1.

This site uses Akismet to reduce spam. Learn how your comment data is processed.