Decipher the mathematical symbols

MATL, 53 bytes

j61tthYX'+-*/'X{[email protected]!"t'ABCD'!XKX{@[email protected]}hwxKGm1L3$).

Uses current version (10.1.0)

EDIT (June 12, 2016): to adapt to changes in the language, replace Y} by g and 1L3$) by Y). The link below incorporates those modifications

Try it online!


This tests all possible permutations of the four operators in a loop until one permutation makes all equations true.

To test if equations are true, a regex is applied to replace the four letters by the operators (in the order dictated by the current permutation), and the string is converted to numbers (evaluated). This gives an array with as many numbers as equations, in which equations that are true become 1 and equations that are false become 0. If this vector only contains 1 values, we're done.

The solution found assigns operators to the four letters, but not all of them necessarily appear in the input. So a final test is done to discard not used letters (and their matching operators).

j            % input data string
61           % '=' (ASCII)
tth          % duplicate twice and concat: '==' (ASCII)
YX           % regexprep to change '=' into '==' in input string
'+-*/'       % push string
X{           % transform into cell array {'+','-','*','/'}
[email protected]!          % all permutations, each in a column
"            % "for" loop. Iterate columns (that is, permutations)
  t          %   duplicate data string containing '=='
  'ABCD'!XK  %   create column array ['A';'B';'C';'D'] and copy to clipboard K
  X{         %   transform into column cell array {'A';'B';'C';'D'} 
  @          %   push column cell array with current permutation of operator symbols
  YX         %   regexprep. Replaces 'A',...,'D' with current permutation of operators
  U          %   convert to numbers, i.e. evaluate string
  ?          %   if all numbers are 1 (truthy result): found it! But before breaking...
    K        %     push column array ['A';'B';'C';'D']
    @Y}      %     push column array with current permutation of operator symbols
    h        %     concatenate horizontally into 4x2 char array
    wx       %     delete original input so it won't be displayed
    K        %     push ['A';'B';'C';'D']
    G        %     push input string
    m        %     logical index that tells which of 'A',...,'D' were in input string
    1L3$)    %     apply that index to select rows of the 4x2 char array
    .        %     we can now break "for" loop
             %   implicitly end "if"
             % implicitly end "for"
             % implicitly display stack contents

Python, 278 characters

My first answer on code golf...

It is just a function implementing a brute force algorithm, you call it passing as argument the string of equations.

from itertools import *
def f(s):
    for p in permutations("+-*/"):
        for v,w in zip(l+["="," "],list(p)+["=="," and "]):
            t=t.replace(v, w)
            if eval(t):
                for c,r in zip(l,p):
                    if c in s:
                return o

JavaScript (ES6), 213 208 bytes

f=(l,s="+-*/",p="",r)=>s?[...s].map(o=>r=f(l,s[g="replace"](o,""),p+o)||r)&&r:l.split` `.every(x=>(q=x.split`=`)[1]==eval(q[0][g](/[A-D]/g,m=>p[(a="ABCD").search(m)])))&&a[g](/./g,(c,i)=>l.match(c)?c+p[i]:"")


Input and output are strings.

Defines a function f which doubles as a recursive function for generating all the permutations of the operators and tests complete permutations with the input equations using eval.

  l,                          // l = input expression string
  s="+-*/",                   // s = remaining operators
  p="",                       // p = current permutation of operators
  r                           // r is here so it is defined locally
  s?                          // if there are remaining operators
    [...s].map(o=>            // add each operator o
        s[g="replace"](o,""), // remove it from the list of remaining operators
        p+o                   // add it to the permutation
        ||r                   // r = the output of any permutation (if it has output)
    &&r                       // return r
  :                           // else if there are no remaining operators
    l.split` `.every(x=>      // for each expression
      (q=x.split`=`)          // q = [ equation, result ]
      [1]==eval(              // if the results is equal to the eval result

        // Replace each letter with the current permutation

    // If all results matched, add permutation symbols to present characters and return


Test does not use default arguments for browser compatibility.

var solution = f=(l,s,p,r)=>s==null&&(s="+-*/",p="",0)?0:s?[...s].map(o=>r=f(l,s[g="replace"](o,""),p+o)||r)&&r:l.split` `.every(x=>(q=x.split`=`)[1]==eval(q[0][g](/[A-D]/g,m=>p[(a="ABCD").search(m)])))&&a[g](/./g,(c,i)=>l.match(c)?c+p[i]:"")
<input type="text" id="input" value="2A2=4 0C0=0 5B0=5 5A0=5" />
<button onclick="result.textContent=solution(input.value)">Go</button>
<pre id="result"></pre>