Brainf*ckish directions Your task – should you choose to accept it – is to build a program that parses and evaluates a string (from left to right and of arbitrary length) of tokens that give directions – either left or right. Here are the four possible tokens and their meanings:

&gt;  go right one single step
&lt;  go left one single step
-&gt; go right the total amount of single steps that you've gone right, plus one,
   before you previously encountered this token and reset this counter to zero
&lt;- go left the total amount of single steps that you've gone left, plus one,
   before you previously encountered this token and reset this counter to zero


There's a catch though – the tokens of directions your program should be able to parse will be presented in this form:

&lt;&lt;-&gt;-&gt;&lt;&lt;-&gt;-&gt;&gt;-&gt;&gt;-&gt;


... in other words, they are concatenated, and it is your program's task to figure out the correct precedence of directions and amount of steps to take (by looking ahead). The order of precedence is as follows (from highest to lowest precedence):


-&gt;
&lt;-
&gt;
&lt;


If you encounter &lt;- when no steps to the left had previously been made since either the start or since the last reset, take one single step to the left. Same rule applies to -&gt;, but then for going to the right.

Your program should start at 0 and its result should be a signed integer representing the final end position.

You may expect the input to always be valid (so, nothing like &lt;---&gt;&gt;--&lt;, for instance).

Example input:

&gt;&lt;-&gt;&gt;&lt;-&lt;-&gt;&lt;-&lt;&gt;&gt;-&gt;


Steps in this example:

 step | token | amount | end position
------+-------+--------+--------------
   1. |   &gt;   |     +1 |           1  
   2. |   &lt;   |     -1 |           0  
   3. |  -&gt;   |     +2 |           2  
   4. |   &gt;   |     +1 |           3  
   5. |   &lt;-  |     -2 |           1  
   6. |   &lt;   |     -1 |           0  
   7. |  -&gt;   |     +2 |           2  
   8. |   &lt;-  |     -2 |           0  
   9. |   &lt;   |     -1 |          -1  
  10. |   &gt;   |     +1 |           0  
  11. |   &gt;   |     +1 |           1  
  12. |  -&gt;   |     +3 |           4  


For clarification: the output of the program should only be the final end position as a signed integer. The table above is just there to illustrate the steps my example took. No need to output such a table, table row, or even just the steps' end positions. Only the final end position, as a signed integer, is required.

Shortest code, after one week, wins.
Python 2.7 - 154 147 134 128 bytes

l=r=p=0
exec"exec raw_input('%s-&gt;','p+=r+1;r=0%s&lt;-','p-=l+1;l=0%s&gt;','r+=1;p+=1%s&lt;','l+=1;p-=1;')"%((";').replace('",)*4)
print p


Serious changes have been made to the way this program works. I've removed the old explanation, which can still be found in this answer's edit history.

This one's gross.

It works pretty much the same way as other answers for this question, replacing the characters in the input with valid statements in that language and executing them. There's one major difference, though: replace is a long word. Screw that.

@ProgrammerDan in chat came up with the idea of using a tuple with the string ;').replace(' in it 4 times, to use the pre-str.format() method of formatting text. Four instances of %s are in the string on the second line, each one taking its value from the associated element of the tuple at the end. Since they're all the same, each %s is replaced with ;').replace('. When you perform the operations, you get this string:

exec raw_input(';').replace('-&gt;','p+=r+1;r=0;').replace('&lt;-','p-=l+1;l=0;').replace('&gt;','r+=1;p+=1;').replace('&lt;','l+=1;p-=1;')


This is now valid python code that can be executed with exec. That's right, baby: Nested execs let me use string operations on code that needs to perform string operations on code. Someone please kill me.

The rest of it is pretty straightforward: Each command is replaced with code that keeps track of three variables: The current position, the number of rights since the last -&gt;, and the same for lefts and &lt;-. The whole thing is run and the position is printed.

You'll notice that I do raw_input(';'), using ';' as a prompt, rather than raw_input() which has no prompt. This saves characters in an unintuitive way: If I did raw_input(), I'd have to have the tuple filled with ).replace(', and every instance of %s would have ';\'' before it except the first one. Having a prompt creates more redundancy so I can save more characters overall.