Skip to content

Instantly share code, notes, and snippets.

@ashearer
Last active August 29, 2019 21:12
Show Gist options
  • Save ashearer/06532783f85374de6960b6ce26c7f248 to your computer and use it in GitHub Desktop.
Save ashearer/06532783f85374de6960b6ce26c7f248 to your computer and use it in GitHub Desktop.
Alphametic puzzle brute force
#!python
from itertools import permutations
def num(format, vals):
return int("".join(str(vals[letter]) for letter in format))
letters = "eightrnwy "
def to_indices(word):
return [letters.find(letter) for letter in word]
eight = to_indices("eight")
three = to_indices("three")
nine = to_indices("nine")
twenty = to_indices("twenty")
for a in permutations(range(10)):
if num(eight, a) + num(three, a) + num(nine, a) == num(twenty, a):
print dict(zip(letters, a))
print num(eight, a), "+", num(three, a), "+", num(nine, a), "=", num(twenty, a)
@ashearer
Copy link
Author

@RocketPuppy I like it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment