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 solution for the equation above is:
9265
+ 1278
——————
10543
Your task
Your task is to write a program or a function which can solve such equations as seen above.
Input
The input is a string in the following format:
[A-Z]+\+[A-Z]+=[A-Z]+
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
You have the following two options for the output format:
- the original equation with the digits substituted
- list of the letters and their values
If there are multiple solutions, any (but only one) of them should be returned. If there are no solutions, the program should return an empty string or null. The output can be returned as a string, can be written to the standard output or a file.
Example:
9265+1278=10543
A=1 B=2 C=3
(you can use any delimiter)
Rules
- To make things easier, numbers are accepted to start with 0, but you can handle numbers with leading 0 as invalid solutions, it's up to you
- Similar letters represent similar digits and different letters represent different digits
- 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?)
- This is a code golf task, shortest code wins. Consecutive whitespace characters count as a single character. (So any program written in whitespace automatically wins)
I have a somewhat hackish solution using 179 chars. If something is not clear, please ask me in the comments.
If there are no solutions, the program should return an empty string or null.
Infinite loops still output nothing ... may I? \$\endgroup\$