{{ message }}

Instantly share code, notes, and snippets.

wo0dyn/maths-puzzle.md

Last active Dec 4, 2015
Can you do the maths puzzle for Vietnamese eight-year-olds that has stumped parents and teachers?

Can you do the maths puzzle for Vietnamese eight-year-olds that has stumped parents and teachers?

Source: article from The Guardian

Script

```from itertools import permutations

numbers = frozenset(range(1, 10))  # = {1...9}
formula = '{} + 13 * {} / {} + {} + 12 * {} - {} - 11 + {} * {} / {} - 10'
stats = {'total': 0, 'matches': 0}

for n in permutations(numbers):
# We are testing every permutations of {1...9}!
stats['total'] += 1

calculation = formula.format(*n)

if eval(calculation) == 66:
# Yeah, I know ``eval``! B/c it's easier to display afterwards…
print(calculation.replace('*', '×').replace('/', '÷') + ' = 66')
stats['matches'] += 1

print('Stats: {matches}/{total}'.format(**stats))```

Result

``````1 + 13 × 2 ÷ 6 + 4 + 12 × 7 - 8 - 11 + 3 × 5 ÷ 9 - 10 = 66
1 + 13 × 2 ÷ 6 + 4 + 12 × 7 - 8 - 11 + 5 × 3 ÷ 9 - 10 = 66
1 + 13 × 3 ÷ 2 + 4 + 12 × 5 - 8 - 11 + 7 × 9 ÷ 6 - 10 = 66
1 + 13 × 3 ÷ 2 + 4 + 12 × 5 - 8 - 11 + 9 × 7 ÷ 6 - 10 = 66
1 + 13 × 3 ÷ 2 + 9 + 12 × 5 - 6 - 11 + 4 × 7 ÷ 8 - 10 = 66
1 + 13 × 3 ÷ 2 + 9 + 12 × 5 - 6 - 11 + 7 × 4 ÷ 8 - 10 = 66
1 + 13 × 3 ÷ 4 + 7 + 12 × 6 - 5 - 11 + 2 × 9 ÷ 8 - 10 = 66
1 + 13 × 3 ÷ 4 + 7 + 12 × 6 - 5 - 11 + 9 × 2 ÷ 8 - 10 = 66
1 + 13 × 3 ÷ 6 + 2 + 12 × 7 - 9 - 11 + 4 × 5 ÷ 8 - 10 = 66
1 + 13 × 3 ÷ 6 + 2 + 12 × 7 - 9 - 11 + 5 × 4 ÷ 8 - 10 = 66
1 + 13 × 3 ÷ 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1 × 3 ÷ 4 - 10 = 66
9 + 13 × 2 ÷ 8 + 7 + 12 × 6 - 5 - 11 + 3 × 1 ÷ 4 - 10 = 66
9 + 13 × 3 ÷ 1 + 6 + 12 × 2 - 5 - 11 + 7 × 8 ÷ 4 - 10 = 66
9 + 13 × 3 ÷ 1 + 6 + 12 × 2 - 5 - 11 + 8 × 7 ÷ 4 - 10 = 66
9 + 13 × 3 ÷ 2 + 1 + 12 × 5 - 6 - 11 + 4 × 7 ÷ 8 - 10 = 66
9 + 13 × 3 ÷ 2 + 1 + 12 × 5 - 6 - 11 + 7 × 4 ÷ 8 - 10 = 66
9 + 13 × 4 ÷ 1 + 5 + 12 × 2 - 7 - 11 + 3 × 8 ÷ 6 - 10 = 66
9 + 13 × 4 ÷ 1 + 5 + 12 × 2 - 7 - 11 + 8 × 3 ÷ 6 - 10 = 66
9 + 13 × 4 ÷ 8 + 5 + 12 × 6 - 7 - 11 + 1 × 3 ÷ 2 - 10 = 66
9 + 13 × 4 ÷ 8 + 5 + 12 × 6 - 7 - 11 + 3 × 1 ÷ 2 - 10 = 66
9 + 13 × 5 ÷ 3 + 1 + 12 × 4 - 2 - 11 + 7 × 8 ÷ 6 - 10 = 66
9 + 13 × 5 ÷ 3 + 1 + 12 × 4 - 2 - 11 + 8 × 7 ÷ 6 - 10 = 66
9 + 13 × 6 ÷ 4 + 3 + 12 × 5 - 8 - 11 + 1 × 7 ÷ 2 - 10 = 66
9 + 13 × 6 ÷ 4 + 3 + 12 × 5 - 8 - 11 + 7 × 1 ÷ 2 - 10 = 66
9 + 13 × 8 ÷ 6 + 2 + 12 × 4 - 1 - 11 + 5 × 7 ÷ 3 - 10 = 66
9 + 13 × 8 ÷ 6 + 2 + 12 × 4 - 1 - 11 + 7 × 5 ÷ 3 - 10 = 66
Stats: 128/362880
``````

Display the grid

```grid = """

┏{h}┓{s}┏{h}┳{h}┳{h}┓{s}┏{h}┓
┃{s}┃{s}┃{s}┃{s}┃{s}┃{s}┃{s}┃
┃{0}┃{s}┃{4}┃{m}┃{5}┃{s}┃{r}┃
┃{s}┃{s}┃{s}┃{s}┃{s}┃{s}┃{s}┃
┣{h}┫{s}┣{h}╋{h}╋{h}┫{s}┣{h}┫
┃{s}┃{s}┃{s}┃{s}┃{s}┃{s}┃{s}┃
┃{p}┃{s}┃{M}┃{s}┃{m}┃{s}┃{e}┃
┃{s}┃{s}┃{s}┃{s}┃{s}┃{s}┃{s}┃
┣{h}┫{s}┣{h}┫{s}┣{h}┫{s}┣{h}┫
┃{s}┃{s}┃{s}┃{s}┃{s}┃{s}┃{s}┃
┃{a}┃{s}┃{b}┃{s}┃{c}┃{s}┃{d}┃
┃{s}┃{s}┃{s}┃{s}┃{s}┃{s}┃{s}┃
┣{h}┫{s}┣{h}┫{s}┣{h}┫{s}┣{h}┫
┃{s}┃{s}┃{s}┃{s}┃{s}┃{s}┃{s}┃
┃{M}┃{s}┃{p}┃{s}┃{p}┃{s}┃{m}┃
┃{s}┃{s}┃{s}┃{s}┃{s}┃{s}┃{s}┃
┣{h}┫{s}┣{h}┫{s}┣{h}┫{s}┣{h}┫
┃{s}┃{s}┃{s}┃{s}┃{s}┃{s}┃{s}┃
┃{1}┃{s}┃{3}┃{s}┃{6}┃{s}┃{8}┃
┃{s}┃{s}┃{s}┃{s}┃{s}┃{s}┃{s}┃
┣{h}╋{h}╋{h}┫{s}┣{h}╋{h}╋{h}┫
┃{s}┃{s}┃{s}┃{s}┃{s}┃{s}┃{s}┃
┃{D}┃{2}┃{p}┃{s}┃{M}┃{7}┃{D}┃
┃{s}┃{s}┃{s}┃{s}┃{s}┃{s}┃{s}┃
┗{h}┻{h}┻{h}┛{s}┗{h}┻{h}┻{h}┛

"""

def formatted_grid(numbers, box_size=9):
"""
Format numbers into the grid.
"""

numbers = (str(n).center(box_size) for n in numbers)
grid_elements = {
# Box elements:
'h': '━' * box_size,
's': ' ' * box_size,
# Arithmetic symbols:
'p': '+'.center(box_size),
'm': '-'.center(box_size),
'M': '×'.center(box_size),
'D': '÷'.center(box_size),
'e': '='.center(box_size),
# Fixed values:
'a': '13'.center(box_size),
'b': '12'.center(box_size),
'c': '11'.center(box_size),
'd': '10'.center(box_size),
'r': '66'.center(box_size),
}

return grid.format(*numbers, **grid_elements)

print(formatted_grid(frozenset(range(1, 10))))```