17
1
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? – Titus – 2018-10-15T15:14:48.887To avoid abusing rule #5; you should rule out whitespace in strings. – Titus – 2018-10-15T15:36:57.057
1All winning answers to this challenge effectively come down to exploiting the whitespace scoring rule, so close-voting as a duplicate. – pppery – 2019-11-09T01:18:09.233
I think the optimal answer is "everything is 0". You might want to specifically prohibit that. – undergroundmonorail – 2014-06-25T01:11:02.910
1What do you mean by everything is 0? Different letters have to denote different numbers. – David Frank – 2014-06-25T01:13:38.370
Missed that, nevermind :) – undergroundmonorail – 2014-06-25T02:23:47.057