Skip to main content
14 of 17
added 31 characters in body
Noodle9
  • 20.3k
  • 3
  • 21
  • 46

C (gcc), 117 113 111 bytes

i;f(char*p){for(i=0;p[i++]-40||p[i+3]-41;);p[i-1]=p[i+1]-62?p[i]:p[i+2];memcpy(p+i,p+i+4,strlen(p));~-i&&f(p);}

Try it online!

Takes an input string with enclosing in parentheses and reduces the input string to either a left (<) or a right (>).

Explanation

i;f(char*p){                       // function taking a string parameter p  
  for(i=0;p[i++]-40||p[i+3]-41;);  // loop until we find chars '(' and ')'   
                                   // separated by 3 characters.   
                                   // since our input is enclosed in 
                                   // parentheses we will always find one.  
  p[i-1]=                          // i has gone forward one but set the 
                                   // first of these characters  - the '('  
                                   // to...     
         p[i+1]-62?                // ...depending on whether the middle   
                                   // char is a '<' or a '>'...   
                   p[i]:           // ...the one before the middle if '<'
                        p[i+2];    // ...the one after the middle if '>'  
  memcpy(p+i,p+i+4,strlen(p));     // move the back end of the string forward  
                                   // over the other 4 chars.    
                                   // now a "(A<B)" is reduced to "A"     
                                   // and a "(A>B)" is reduced to "B" 
  ~-i&&                            // if we didn't just reduce the start of p     
       f(p);                       // then recursively call f.   
                                   // otherwise p is reduced to the answer 
                                   // and we're done.     
 }  
Noodle9
  • 20.3k
  • 3
  • 21
  • 46