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<S<L
v ^
S R>L
v ^
L>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