Vyxal is a stack-based language, meaning that everything operates by popping and pushing values onto a stack. It has a bunch of useful flags, one of which is r
.
Running a Vyxal program with the r
flag causes functions to take their elements in reverse order.
For example, the program 5 3 -
means: Push 5 to stack, push 3 onto the stack, and subtract them by popping 3, popping 5, and subtracting the 3 from the 5 to yield 2. But with the r
flag, it first pops the 5, then the 3, and subtracts 5 from 3 to yield -2.
For this challenge, we will be operating within a much-simplified version of Vyxal.
The digits 0-9 each push themselves to the stack , and lowercase letters are dyadic functions, meaning they pop two values from the stack and do something with them, before pushing the result. Everything else does nothing and won't be included in the input.
Your challenge is to take a program with this format and output a program that would do the same thing if the input order of each function is reversed.
This sounds complicated, so let's demonstrate with this example:
87a
To do this, we simply swap the 8 and the 7 to yield 78a
.
A more complicated example:
54a78bc
How it works:
c # c is a function taking...
78b # This, where:
b # b is a function taking
78 # 7 and 8
# And...
54a # This, where:
a # a is a function taking...
54 # 5 and 4
So to flip it, we just flip the operands of every function:
<-->
<->|| c
|| 78b
54a |
| |
<-->
Yielding:
87b45ac
Which is the result!
Another way to explain this is:
An operator is one of a-z
A token is either a single digit, or of the format [TOKEN][TOKEN][OPERATOR]
For each token of the format [TOKEN][TOKEN][OPERATOR], flip the two tokens.
Scoring
This is code-golf, shortest wins!
Testcases
You can assume there will always be two values to pop whenever there's a function/operator.
123ab => 32a1b
59a34bz => 43b95az
123456abcde => 65a4b3c2d1e
1 => 1
12a53bx7c08d9ef => 980de735b21axcf
12a34bc56d78efg => 87e65df43b21acg
As usual, these are created by hand, so please tell me if they're wrong.