Given an expression matching the regex /^[-~]*x$/
, evaluate it in terms of x
and output a string matching the regex /^-?x[+-]\d+$/
.
For example, the string -~x
evaluates to x+1
, while the string -~-x
evaluates to -x+1
, and the string -~-~--x
evaluates to x+2
.
We start from x
and evaluate the string right to left. -
negates the term, while ~
converts y
to -y-1
.
Testcases:
x x+0
~x -x-1
-~x x+1
~-~x -x-2
-~-~x x+2
--~~x x+0
~-x x-1
-~-x -x+1
This is code-golf. Shortest answer in bytes wins.
The input/output format is strict. The "x"
is mandatory.
x+010
instead ofx+10
for-~-~-~-~-~-~-~-~-~-~x
? It matches the second regex. \$\endgroup\$~
while it hasn't been defined \$\endgroup\$