Area enclosed by perimeter loop Find the area of a region of unit cells given its perimeter loop as a sequence of 90-degree turns.

For example, take the three-cell region 

XX
X


whose perimeter loop we draw

L&lt;S&lt;L
v   ^
S R&gt;L
v ^
L&gt;L


Each turn is marked as left (L), straight (S), or right (R). Starting from the R, the turns are RLLSLSLL. So, given input RLLSLSLL, we should output 3 for the area.

The input sequence is guaranteed to trace out a loop enclosing a single region on its left.


The path ends back at the start point, facing the initial direction, forming a loop.
The loop does not cross or touch itself.
The loop goes counterclockwise around a region.


I/O

You can take input as a list or string of characters LSR, or as  numbers -1, 0, 1 for left, straight, right. Output is a positive integer. Floats are OK.

Test cases

The inputs are given in both formats followed by their respective outputs.

RLLSLSLL
LLLL
SLLSLL
LSRRSLLSSLSSLSSL
SSSSSLSSSSSLSSSSSLSSSSSL

[1, -1, -1, 0, -1, 0, -1, -1]
[-1, -1, -1, -1]
[0, -1, -1, 0, -1, -1]
[-1, 0, 1, 1, 0, -1, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1]
[0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, -1]

3
1
2
7
36
Brain-Flak, 112 bytes

(([]){[{}]&lt;{({}()){{}&lt;&gt;([{}]&lt;([{}])&gt;)(&lt;&gt;)}&lt;&gt;(({}[({})])[({}{})])&lt;&gt;}{}&lt;&gt;&gt;({}&lt;({}())&gt;)&lt;&gt;([])}{})({()&lt;({}()())&gt;}{})


Try it online!

This program uses Green's theorem to compute the area

The current location is stored on the right stack, in a format that depends on the direction faced.

Direction  top  second
north       -x       y
west        -y      -x
south        x      -y
east         y       x


In all instances, the second value on the stack will increase by 1, and the line integral for the area decreases by half the value on the top of the stack.  To compensate, the end of the program divides the running total by -2.

# For each number in input
(([]){[{}]

  # Evaluate turn-handling to zero
  &lt;

    # If turn:
    {

      # If right turn:
      ({}()){{}

        # Negate both values on other stack (reverse direction)
        &lt;&gt;([{}]&lt;([{}])&gt;)

      (&lt;&gt;)}

      # Swap the two stack elements and negate the new top of stack
      # This performs a left turn.
      &lt;&gt;(({}[({})])[({}{})])&lt;&gt;

    }{}

  &lt;&gt;&gt;

  # Evaluate as top of stack and...
  ({}&lt;

    # increment the number below it
    ({}())

  &gt;)&lt;&gt;

([])}{})

# Divide total by -2
({()&lt;({}()())&gt;}{})