3
\$\begingroup\$

This question already has an answer here:

Task

The letters spell out actual words, but if you replace each letter with a digit from 0–9, it also “spells” an arithmetic equation. The trick is to figure out which letter maps to each digit. All the occurrences of each letter must map to the same digit, no digit can be repeated, and no “word” can start with the digit 0.

Input

HAWAII + IDAHO + IOWA + OHIO = STATES

Output

510199 + 98153 + 9301 + 3593 = 621246

Input

I + LOVE + YOU = DORA

Output

1 + 2784 + 975 = 3760

Input

SEND + MORE = MONEY

Output

9567 + 1085 = 10652

Rules

  • Standard loopholes apply.
  • shortest code wins.
  • addition only

Appendix

for other Inputs you gotta keep in mind you can only have 10 different characters(to assign values 0-9)

\$\endgroup\$

marked as duplicate by Erik the Outgolfer, Mr. Xcoder, Peter Taylor code-golf Oct 17 '17 at 11:13

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    \$\begingroup\$ @Adám you can but don’t have to handle spaces, your input is also fine \$\endgroup\$ – 0x45 Oct 17 '17 at 10:59
  • 1
    \$\begingroup\$ Also this had no winning criterion. I added code-golf. \$\endgroup\$ – Mr. Xcoder Oct 17 '17 at 11:06
  • 1
    \$\begingroup\$ What inputs are acceptable? Can we take the input as e.g. [[[S,E,N,D],[M,O,R,E]],[M,O,N,E,Y]]? \$\endgroup\$ – Fatalize Oct 17 '17 at 11:07
  • 1
    \$\begingroup\$ Also can we assume that such a mapping is possible? \$\endgroup\$ – Mr. Xcoder Oct 17 '17 at 11:08
  • 1
    \$\begingroup\$ Will there only be upper case letters A-Z? Can we choose lower case if we want? \$\endgroup\$ – Stewie Griffin Oct 17 '17 at 11:13
2
\$\begingroup\$

JavaScript (ES7), 104 bytes

f=(s,o,c=s.match(/[A-Z]/))=>c?[...2**29+'4'].some(i=>o&1<<i?0:f(r=s.split(c).join(i),o|1<<i))&&r:eval(s)

Demo

f=(s,o,c=s.match(/[A-Z]/))=>c?[...2**29+'4'].some(i=>o&1<<i?0:f(r=s.split(c).join(i),o|1<<i))&&r:eval(s)

console.log(f('I + LOVE + YOU == DORA'))

Formatted and commented

f = (                               // f = recursive function taking:
  s,                                //   s = input string
  o,                                //   o = bitmask of used digits
  c = s.match(/[A-Z]/)              //   c = next alphabetical character in s
) =>                                //       (either null or a single-element array)
  c ?                               // if c is not null:
    [...2**29 + '4'].some(i =>      //   for each character i of '5368709124'
      o & 1 << i ?                  //     if this digit was already used:
        0                           //       ignore this iteration
      :                             //     else:
        f(                          //       do a recursive call to f() with:
          r = s.split(c).join(i),   //         all characters c replaced with i
          o | 1 << i                //         the updated bitmask
        )                           //       end of recursive call
    ) && r                          //   end of some(): if successful, return r
  :                                 // else:
    eval(s)                         //   evaluate this fully numerical expression
\$\endgroup\$
  • 1
    \$\begingroup\$ 104, since == is allowed \$\endgroup\$ – 0x45 Oct 17 '17 at 11:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.