# Solve For X

You will be given an equation as a string and you will need to [solve for X](https://www.mathplacementreview.com/algebra/basic-algebra.php#solve-for-a-variable) and return x's value. For example: 

```python
solve_for_x('x - 5 = 20') # should return 25
solve_for_x('20 = 5 * x - 5') # should return 5
solve_for_x('5 * x = x + 8') # should return 2
solve_for_x('(5 - 3) * x = x + 2') # should return 2
```

NOTES:
 * All numbers will be whole numbers
 * Don't forget about the [order of operations](https://www.mathplacementreview.com/algebra/basic-algebra.php#order-of-operations).
 * If the random tests don't pass the first time, just run them again.

```python

USER_FILENAME = "<user_code>"
import sys, json, builtins
import math, heapq, bisect, itertools, functools, collections, statistics, fractions, decimal, operator, array, random, re, string, os, pathlib
try:
    import resource as _res
except Exception:
    _res = None
from collections import Counter, defaultdict, deque, OrderedDict
from functools import lru_cache, reduce
from itertools import accumulate, permutations, combinations, product
from math import gcd, ceil, floor, sqrt
from decimal import Decimal
try:
    from typing import *
except Exception:
    pass
try:
    sys.setrecursionlimit(1_000_000)
except Exception:
    pass
try:
    import site as _site
    try:
        _sys_sites = _site.getsitepackages() if hasattr(_site, "getsitepackages") else []
        for _p in _sys_sites:
            if _p and _p not in sys.path:
                sys.path.append(_p)
    except Exception:
        pass
    try:
        _usp = _site.getusersitepackages()
        if isinstance(_usp, str):
            _usp = [_usp]
        for _p in _usp:
            if _p and _p not in sys.path:
                sys.path.append(_p)
    except Exception:
        pass
except Exception:
    pass

def _disabled_input(*args, **kwargs):
    raise RuntimeError("input() is disabled in callable mode")
builtins.input = _disabled_input

ns = {"__name__": "__judge__"}  # prevent if __name__ == "__main__" blocks
for _k, _v in list(globals().items()):
    if not _k.startswith("_"):
        ns[_k] = _v
_CODE = 'class Exp:\n    def __init__(self, s):\n        self.a = 1 \n        self.p = 0\n        if s[-1] == \'x\':\n            self.p = 1\n            s = s[:-1]\n        if 0 < len(s):\n            self.a *= int (s)\n    \n    def __add__(self, other):\n        if self.p == other.p:\n            self.a += other.a\n            return self\n        else:\n            return Equ([self, other])\n    \n    def __sub__(self, other):\n        if self.p == other.p:\n            self.a -= other.a\n            return self\n        else:\n            return Equ([self, Exp("-1") * other])\n\n    def __mul__(self, other):\n        self.p += other.p\n        self.a *= other.a\n        return self\n    \n    def __div__(self, other):\n        self.p -= other.p\n        self.a /= other.a\n        return self\n        \n    def __str__(self):\n        s = ""\n        if self.a != 0:\n            s += str(self.a)\n        if (self.p) == 1:\n            s += \'x\'\n        if s == "":\n            s += \'0\'\n        return s\n        \n\nclass Equ:\n    def __init__(self, exp):\n        self.exp = dict()\n        for e in exp:\n            if e.p not in self.exp:\n                self.exp[e.p] = e\n            else:\n                self.exp[e.p] += e\n        \n    def __add__(self, other):\n        if type(other) == Exp:\n            other = Equ([other])\n        for p in other.exp:\n            if p in self.exp:\n                self.exp[p] += other.exp[p]\n            else:\n                self.exp[p] = other.exp[p]\n        return self\n\n    def __sub__(self, other):\n        if type(other) == Exp:\n            other = Equ([other])\n        for p in other.exp:\n            if p in self.exp:\n                self.exp[p] -= other.exp[p]\n            else:\n                self.exp[p] = Exp("-1") * other.exp[p]\n        return self\n                \n    def __mul__(self, other):\n        if type(other) == Exp:\n            other = Equ([other])\n        res = None\n        for p1 in other.exp:\n            temp_res = []\n            for p2 in self.exp:\n                temp_res.append(self.exp[p2] * other.exp[p1])\n            if res is None:\n                res = Equ(temp_res)\n            else:\n                res += Equ(temp_res)\n        return self\n\n    def __div__(self, other):\n        if type(other) == Exp:\n            other = Equ([other])\n        res = None\n        for p1 in other.exp:\n            temp_res = []\n            for p2 in self.exp:\n                temp_res.append(self.exp[p2] / other.exp[p1])\n            if res is None:\n                res = Equ(temp_res)\n            else:\n                res += Equ(temp_res)\n        return self\n        \n    def __str__(self):\n        s = ""\n        for p in self.exp:\n            s += \' (\' + str(self.exp[p]) + \') +\'\n        return s[:-1]\n\n    def get_power(self, p):\n        return self.exp[p] if p in self.exp else Exp("0") if p==0 else Exp("0x")\n\n\ndef build1(s):\n    s = s.replace(\' \', \'\')\n    stack = []\n    s += \'!\'\n    if s[0] == \'-\':\n        s = \'0\' + s\n    j = 0\n    for i in range(len(s)):\n        if s[i] in [\'+\',\'-\',\'*\',\'/\',\'(\',\')\',\'!\']:\n            if j < i:\n                stack.append(s[j:i])\n            stack.append(s[i])\n            j = i+1\n    stack.remove(\'!\')\n    for i, x in enumerate(stack):\n        if x not in [\'+\',\'-\',\'*\',\'/\',\'(\',\')\',\'!\']:\n            stack[i] = Exp(x)\n    return stack\n    \ndef build2(s):\n    while \')\' in s: \n        end = s.index(\')\')\n        start = end\n        while s[start] != \'(\':\n            start -= 1\n        s = s[:start] + [build2(s[start+1:end])] + s[end+1:]\n    op = {\'+\': lambda x, y: x + y,\n          \'-\': lambda x, y: x - y,\n          \'*\': lambda x, y: x * y,\n          \'/\': lambda x, y: x.__div__(y) }\n    i = 2\n    for order in [0, 1]:\n        i = 2\n        while i < len(s):\n            if (order == 0 and s[i-1] in [\'*\', \'/\']) \\\n            or (order == 1 and s[i-1] in [\'+\', \'-\']):\n                s[i-2] = op[s[i-1]](s[i-2], s[i])\n                s.pop(i)\n                s.pop(i-1)\n            else:\n                i += 2\n    return s[0]\n        \ndef build(s):\n    stack = build1(s)\n    equ = build2(stack)\n    if type(equ) == Exp:\n        equ = Equ([equ])\n    return equ\n  \n\ndef solve_for_x(equation):\n    l, r = equation.split(" = ")\n    l, r = build(l), build(r)\n    l, r = l.get_power(1) - r.get_power(1), r.get_power(0) - l.get_power(0)\n    return r.a / l.a'
_co = compile(_CODE, USER_FILENAME, "exec")
exec(_co, ns, ns)

_target = None
_fn_name = 'solve_for_x'
if _fn_name:
    if "Solution" in ns:
        try:
            _obj = ns["Solution"]()
            if hasattr(_obj, _fn_name):
                _target = getattr(_obj, _fn_name)
        except Exception:
            _target = None
    if _target is None and _fn_name in ns and callable(ns[_fn_name]):
        _target = ns[_fn_name]
# Fallback: if there's exactly one user-defined function, use it
if _target is None:
    _cands = [v for k, v in ns.items() if callable(v) and getattr(v, "__module__", "") in ("__judge__", "__main__")]
    _user_funcs = [c for c in _cands if getattr(c, "__name__", "").startswith("_") is False]
    if len(_user_funcs) == 1:
        _target = _user_funcs[0]
if _target is None:
    raise RuntimeError(f"Could not resolve callable '{_fn_name}'")

_raw = sys.stdin.read().strip()
if _raw:
    _parsed = json.loads(_raw)
    if isinstance(_parsed, dict):
        _kwargs = _parsed.get("kwargs", {})
        _args = _parsed.get("args", [])
    else:
        _args, _kwargs = _parsed, {}
else:
    _args, _kwargs = [], {}
if not isinstance(_args, (list, tuple)):
    _args = [_args]
_res = _target(*_args, **_kwargs)
try:
    sys.stdout.write(repr(_res) + "\n")
except Exception:
    pass

```

Measured memory_bytes: 10435