In this challenge, you'll calculate how great your land is.
Write a program or function that calculates the size of your land, given a wall you have built. You're given a non-empty input string containing a set of 4 distinct characters of your choice that represent the four directions "up", "down", "left" and "right" (I'll use ^ v < >
in this challenge). It's not possible to take 180 degree turns (<>
or ^v
), but you may cross your wall.
The way you "capture" land is by encircling it with your wall. The wall itself is also considered part of your land. A few examples will make it more clear. I'll use o
for land that has been encircled by the wall, x
for the wall itself, and S
for the starting point of the wall, just to illustrate how the wall is built. The output should be the total size of your land (the number of o
, x
and S
in the test cases below).
Input: >>>>
Land: Sxxxx
Output: 5
Input: <<<^^^>>>vv
Land:
xxxx
xoox
xoox
xxxS
Output: 16
Input: <<<^^^>>>v
Land:
xxxx
x x
x
xxxS
Output: 11
Input: <
Land: xS
Output: 2
Input: >>>>>>vvvvvvvvv<<<<<^^^^>>>>>>>>vvvvvvvvvv<<<<<<<<<<<<<<<^^^^^^^^^>>>vvvvvv<<<<<
Land:
Sxxxxxx
x
x
x
x
xxxxxxxxx
xxxx xoooox x
xoox xoooox x
xoox xoooox x
xoox xxxxxx x
xoox x
xoox x
xxxxxx x
x x
x x
xxxxxxxxxxxxxxxx
Output: 101
Input: >>vvvv>>^^<<<<^
Land:
Sxx
xox
xxxxx
xox
xxx
Output: 17
Input: <<^^^>>>vv
Land:
xxxx
x x
x x
xxS
Output: 11 <- Note, diagonal edges do not close the "loop"
Clarifications:
- You do not need to draw the wall, the output should only be an integer
- The input format is optional. You may take a string with
<>^v
, a list of digits,(1, -1, i, -i)
, a list of charactersABCD
etc.
This is code-golf so the shortest code in each language wins. Remember, explanations are important, even in "regular" languages!