Rating:

```
#!/usr/bin/env python

'''
Rules:
() + () = ()() => [combine]
((())) + () = ((())()) => [absorb-right]
() + ((())) = (()(())) => [absorb-left]
(())(()) + () = (())(()()) => [combined-absorb-right]
() + (())(()) = (()())(()) => [combined-absorb-left]
(())(()) + ((())) = ((())(())(())) => [absorb-combined-right]
((())) + (())(()) = ((())(())(())) => [absorb-combined-left]
() + (()) + ((())) = (()()) + ((())) = ((()())(())) => [left-associative]

Example:
(()) + () = () + (()) = (()())
'''

from pwn import *


def get_max_depth(s):
d = 0
max_d = 0

for ss in s:
if ss == '(':
d += 1

if ss == ')':
d -= 1

if d > max_d:
max_d = d

return max_d

def add_nodes(n1, n2):
d1 = get_max_depth(n1)
d2 = get_max_depth(n2)

if d1 < d2:
return n2[0] + n1 + n2[1:]
elif d1 == d2:
return n1 + n2
else:
return n1[:-1] + n2 + n1[-1]

def main():
r = remote('2018shell1.picoctf.com', 7866)

ci = 1
while True:
lines = r.recvrepeat(0.2).split('\n')

print '\n'.join(lines)

if 'pico' in '\n'.join(lines):
break

q = lines[-3].split('=')[0].strip()
log.info('Got challenge #{}: {}'.format(ci, q))
q = q.split()

queries = { }

while len(q) > 1:
n1 = q.pop(0)
q.pop(0)
n2 = q.pop(0)

n = add_nodes(n1, n2)

q = [n] + q

log.info('Sending: {}'.format(q[0]))
r.sendline(q[0])

ll = r.recvuntil('\n')

print ll

ci += 1

if __name__ == '__main__':
main()
```