This question is similar to Write a word equation solver by David Frank but different in three important ways: (1) David asked for just the first solution found, and I am asking for all the solutions. (2) I am asking to output the time required to solve the puzzle. And (3), it is a completely different undertaking to write a program designed to be fast compared with one designed to be short. A brute force method is probably best for code golf, but will not beat my ?amazing? time of 0.15 seconds. -I say "amazing" only to encourage other to try it out. I'm sure mine won't be the fastest.
(I'm going to copy a lot of the text from that question.)
Introduction
Consider the following example:
CODE
+ GOLF
——————
GREAT
This is an equation where each letter represents a decimal digit and the words represent natural numbers (similar letters represent similar digits and different letters represent different digits). The task is to match each letter with its digit value so that the equation is correct. One of the four solutions for the equation above is:
9265
+ 1278
——————
10543
Your task
Your task is to write a program or function to find ALL of the answers to such a word equation. Your program should output all the answers and the time it took to find them in seconds.
Input
The input will be something like this, two words added together to make a third word.
Example:
- CODE+GOLF=GREAT
- AA+BB=CC
Spaces are omitted and only letters between capital A and Z will be used (no special or small letters). This string can be read from the standard input, from a file, or as a function parameter.
Output
The output should look something like this. To take luck out of the competition, your program must list all the solutions.
time = XX.XXX seconds
(1) 9265+1278=10543
(2) 9275+1268=10543
(3) 9428+1437=10865
(4) 9438+1427=10865
Rules
- Similar letters represent similar digits and different letters represent different digits
- To make things easier, you can assume that numbers can or don't start with a leading zero, it's up to you. The four answers I got for the sample problem assume numbers do not start with leading zeros.
- You can use any language and the standard library of the chosen language (no external libs)
- You cannot connect to any resources on the internet (why would you anyways?)
- So the goal here is speed, but of coarse that depends on what computer you use. In order to account for the different speed of different computers, you could run my example on your computer and then report how many times faster or slower yours is than my sample answer.
- Please explain how your code works.
Example in JavaScript