Rating:

# picoCTF 2018: script me
***Category: Misc***
>*Can you understand the language and answer the questions to retrieve the flag? Connect to the service with nc 2018shell2.picoctf.com 1542*
## Solution

Upon connecting, you are given a set of rules:
```
Rules:
() + () = ()() => [combine]
((())) + () = ((())()) => [absorb-right]
() + ((())) = (()(())) => [absorb-left]
(())(()) + () = (())(()()) => [combined-absorb-right]
() + (())(()) = (()())(()) => [combined-absorb-left]
(())(()) + ((())) = ((())(())(())) => [absorb-combined-right]
((())) + (())(()) = ((())(())(())) => [absorb-combined-left]
() + (()) + ((())) = (()()) + ((())) = ((()())(())) => [left-associative]

Example:
(()) + () = () + (()) = (()())
```
After a bit of messing around, I was able to figure out how this "language" worked. The key to solving was comparing the max depth of each set of parentheses.
Although there are 8 rules given, the rules can be broken down to the four basic rules:

**Combine**

If the two have equal depths, just put them together.

**Absorb-Right**

If the left set has a larger depth than right set, take the right set and place it in the right side of the outer-most set of parentheses of the left set.

**Absorb-Left**

If the right set has a larger depth than the left set, take the left set and place it in the left side of the outer-most set of parentheses of the right set.

**Left-Associative**

Simply start from the leftmost set and work your way to the right.

This is what the absorb left/right functions look like in python:
```python
def absorbl(l,r):
return r[0]+l+r[1:]

def absorbr(l,r):
return l[:-1]+r+l[-1]
```

There is no need to write a function for the combine rule since you can just simply concatenate the two sets. To solve, just simply take the equation and work from left to right using the three operations. The full script can be found [here](solve.py).

***Flag: `picoCTF{5cr1pt1nG_l1k3_4_pRo_0466cdd7}`***

Original writeup (https://github.com/scai16/CTF/tree/master/2018/PicoCTF%202018/script%20me).