Alphametics

Alphametics are puzzles in which letters are substituted for the digits in an arithmetic calculation, and the words formed by the operands and answer produce a sensible phrase. For example - The goal is to find the digits that can be substituted for the letters to produce a correct addition statement. The answer for this puzzle is given below. Wherever an 'a' appears in the word form, the digit 2 is substituted in the number form. Wherever an 's' appears in the word form, the digit 3 is used in the number form. The complete mapping of characters to digits appears to the right. How about the much simpler example below? Here are a bunch of guesses - The last guess above is the answer.

Solve the puzzle with a program

Is it possible to automate the process of solving alphametic puzzles?   What steps would be necessary and sufficient to address this challenge?   How does a person solve this kind of puzzle?
  1. Start with one character, assign it a digit, and "mark" that digit as taken.
  2. Repeat step 1 until a dead-end is reached.
  3. Somehow record this failed mapping or path to avoid "passing this way again".
  4. Either start from scratch, or backtrack to a previous decision point, and try a different choice.
We could certainly design a silicon-based (i.e. computer) algorithm that reproduces the carbon-based (i.e. human) algorithm given above.   Or  –  we could create a blunt, brute-force procedure that leverages the computer's unique aptitude for testing thousands or millions of hypotheses very rapidly.

How about identifying all possible guesses to this problem, and then testing each guess in turn, until the answer is found?

So  –  is the number of all possible guesses finite?   Is it knowable?   Answer

There are 10 digits to choose from (0 through 9), and there are 3 letters in this puzzle ('b', 'i', 'l') that need to be assigned a number.   How could the problem be stated in order to lead us to a formula that gives the total number of guesses possible?   Answer

If we were to enumerate all combinations of 10 digits taken 3 at a time, we would find there are 120.   If we were to then list all possible orderings (i.e. permutations) of a 3 digit number, we would find there are 6.   The total possible guesses is 120 * 6 = 720 (all permutations of all combinations).

Here is a Java program that produces the list of all possible guesses. The source for the Combinations and Permutations classes can be found  here.

Now that we have a framework for generating all possible guesses, how do you want to proceed?

..... [Final Jeopardy music goes here] .....

Somehow, we need to take each guess, and evaluate it against the statement  "i + bb = ill".   Let's decide on the convention that

To convert the word  "ill"  into its corresponding 3-digit number, we need a formula like - To convert  "bb", we need the formula We can now test each possible guess with something like - Adapting the previous program might produce something like - And the output is -

A new puzzle

Let's try a new puzzle - What is the first step?   Answer

There are 7 unique characters that need to be decoded -

What needs to be changed in the program to solve the new puzzle?   Answer

Using the previous strategy, the three words of this puzzle can be represented as -

The new program looks remarkably similar - And the output is - The first two solutions are less than desirable.   The program could be upgraded to filter out these "false hits".   Or - the program's User Guide could simply describe this as a  feature.

A more general approach

It sure would be nice if our program could support all addition alphametic puzzles without having to rewrite it for each puzzle.   Here is a sample of how this "brave new world" might function - How would you go about creating this program that can program itself?

What steps are necessary and sufficient?

How about these steps?

impl