Skip to main content
6 of 6
saved 1 byte
Arnauld
  • 197.7k
  • 20
  • 179
  • 650

C (gcc),  119 ... 111  110 bytes

Outputs by modifying the input string.

p;f(char*s){for(char*r,*q=s;*q;p?*s++=*q++:(q=r))for(r=q,p=0;*r?p+=(*r%17?strlen(q):1)*~-(*r++%5&2):0;);*s=0;}

Try it online!

###How?

The algorithm is the same used in my JS answer with a few differences:

  • We use a for loop instead of a recursive approach.

  • We overwrite the input string with the output. This is safe because what is written is at most as long as what is read, and the meaningful information is always ahead of both the read and the write pointers (q and s respectively).

  • Given the ASCII code c of the move character, we use c % 17 to find out if it's a vertical or horizontal move, and c % 5 & 2 to distinguish between down and up or between left and right.

               | 'D' (68) | 'L' (76) | 'R' (82) | 'U' (85)
      ---------+----------+----------+----------+----------
       % 17    |     0    |     8    |    14    |     0
       % 5 & 2 |     2    |     0    |     2    |     0
    
Arnauld
  • 197.7k
  • 20
  • 179
  • 650