# Enigmatic Code

Programming Enigma Puzzles

## Puzzle 42: Football – four teams

From New Scientist #1093, 9th March 1978 [link]

Four football teams are to play each other once. After some of the matches had been played a table was drawn up giving some details of the matches played, won, lost, etc. But unfortunately Uncle Bungle had been at it again and the digits (from 0 to 9) had been replaced by letters. Each letter stood for the same digit wherever it appears and different letters stood for different digits.

The table looked like this:

(Two points are given for a win, and one point to each side in a drawn match).

Find the score in each match.

[puzzle42]

### One response to “Puzzle 42: Football – four teams”

1. Jim Randell 8 August 2018 at 3:46 pm

This Python program uses the [[ `Football()` ]] helper class from the enigma.py library. It runs in 163ms.

Run: [ @repl.it ]

```from itertools import permutations
from enigma import Football, irange, update

# scoring system
football = Football(points=dict(w=2, d=1))

# labels for the teams
(A, B, C, D) = (0, 1, 2, 3)

# the table
table = dict(played='pt??', w='???r', d='kp?k', points='???x')

# allowable digits
digits = set(irange(0, 9))

# determine the outcomes in the matches
for (ms, d) in football.substituted_table(table):

# choose values for h and y
for (h, y) in permutations(digits.difference(d.values()), 2):

d1 = update(d, 'hy', (h, y))

# look for scores in matches for A and C
for ss in football.substituted_table_goals('h?ym', 'xhx?', ms, d=d1, teams=[A, C]):

# calculate goals for/against B, D in matches except for B vs D
(fB, aB) = football.goals([ss[(A, B)], ss[(B, C)]], [1, 0])
(fD, aD) = football.goals([ss[(A, D)], ss[(C, D)]], [1, 1])

# goals scored by D against B in the B vs D match
DB = h - aB
if DB < 0: continue

m = fD + DB
if m not in digits or m in d1.values(): continue

# the goals scored by B against D in the B vs D match
if ms[(B, D)] == 'l':
BDs = irange(0, DB - 1)
else:
# we don't need further code here (although 'd' and 'x' can
# easily be handled, 'w' would be more problematic)
raise NotImplementedError()

# output matches and possible scores
for BD in BDs:
football.output_matches(ms, update(ss, [((B, D), (BD, DB))]), teams="ABCD", d=update(d1, [('m', fD)]))
```

Solution: The scores in the played matches are: A vs D = 5 – 6; B vs C = 4 – 4; B vs D = 0 – 1; C vs D = 0 – 2. The A vs B and A vs C matches are not yet played.

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