# How great is your land?

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 characters ABCD etc.

This is so the shortest code in each language wins. Remember, explanations are important, even in "regular" languages!

• You should change the description so that it calculates how many clovers you have enclosed :P Mar 17 '17 at 19:45
• Somewhat related Mar 17 '17 at 20:15
• Related? Mar 18 '17 at 14:11
• @MatthewRoh, hmmm. Mar 18 '17 at 20:03
• @Stewie Oh yes, that's related too Mar 19 '17 at 2:05

# Python 2, 385345 332 bytes

A,I,R=max,min,range
a=b=0
p=[[a,b]]
for i in input():a+=i%2*(2-i);b+=(1-i%2)*(1-i);p+=[a,b],
k,l=zip(*p)
x=A(k)-I(k)+3
y=A(l)-I(l)+3
o=[[1]*y for _ in' '*x]
def g(m,n):
if 0<o[m][n]and[m+I(k)-1,n+I(l)-1]not in p:o[m][n]=0;[g(i,j)for i in R(A(0,m-1),I(x,m+2))for j in R(A(0,n-1),I(y,n+2))if(i,j)!=(m,n)]
g(0,0)
print sum(map(sum,o))


Input is numerical, 0~3, the 0-index of the symbols here: >v<^

#starting position
a,b=0
#new list to hold the wall coordinates
p=[[a,b]]

#iterate over the input calculating
#the next coordinate and storing on p
for i in input():
a=a+i%2*(2-i)
b=b+(1-i%2)*(1-i)
p+=[[a,b]]
#i%2*(2-i) and (1-i%2)*(1-i) generate the increment
#of each symbol from last position
# >/0 : (0,1)
# v/1 : (1,0)
# </2 : (0,-1)
# ^/3 : (-1,0)

#transpose the coordinate list
k,l=zip(*p)
#calculate the difference between the max and min values
#to generate the total land size
x=max(k)-min(k)+3
y=max(l)-min(l)+3

#create a matrix of 1's with the total land size
o=[([1]*y) for _ in ' '*x]

#recursive function that sets a cell to 0
#and call itself again on all surrounding cells
def g(m,n):
#correct the indexes (like negative ones)
a,b=m+min(k)-1,n+min(l)-1
#if this cell contains 1 and don't belong to the wall
if o[m][n]>0 and (a,b) not in p:
#sets to 0
o[m][n]=0
#call again on surrounding cells
for i in range(max(0,m-1),min(x,m+2)):
for j in range(max(0,n-1), min(y,n+2)):
if (i,j)!=(m,n):g(i,j)

#call the recursive function o origin
g(0,0)
#print the sum of the cells
print sum(map(sum,o))


This is the resulting matrix:

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


# Octave, 838583 79 bytes

@(p)nnz(bwfill(accumarray([real(c=cumsum([0;p])) imag(c)]+nnz(p)+1,1),"holes"))


Try it on Octave Online!

A function that takes as input a column vector containing (1, -1, i, -i)

Using approach of @lanlock4 's Mathematica answer adding length of the input to coordinates to avoid non positive coordinates, instead of subtracting min of coordinates from them. Saved 4 bytes.

@(p)nnz(bwfill(accumarray((k=[real(c=cumsum([0;p])) imag(c)])-min(k)+1,1),"holes"))


Try it on Octave Online!

Changed for better visualization.

Explanation:

%compute position of walls
c= cumsum([0;p]) % p should be top padded with a 0
row = real(c);
col = imag(c);
k = [row col];

%offset positions so all positions become positive
pos = k - min(k) +1;
%create a binary array that is 1 for walls and 0 elsewhere
bin = ~~accumarray(pos,1);

*******
*
*
*
*
*********
****   *    *  *
*  *   *    *  *
*  *   *    *  *
*  *   ******  *
*  *           *
*  *           *
******           *
*              *
*              *
****************

%use flood fill to fill holes
filled = bwfill(bin, 'holes');

*******
*
*
*
*
*********
****   ******  *
****   ******  *
****   ******  *
****   ******  *
****           *
****           *
******           *
*              *
*              *
****************

%count number of ones in the filled image
result = nnz(filled)


y=length
i=filter
u i e l=take i l++[e]++drop(i+1)l
k v(r,c)g=u r(u c v(g!!r))g
b(r,c)g=g!!r!!c
w(r,c)s g=case s of{""->j;'<':t->w(r,c-1)t j;'>':t->w(r,c+1)t j;'v':t->w(r+1,c)t j;'^':t->w(r-1,c)t j}where j=k 2(r,c)g
e[]v g=g;e(x:d)v g|elem x v||b x g/=1=e d v g|b x g==1=e(d++(i(\x->notElem x v)$i(\(r,c)->r>=0&&c>=0&&r<y g&&c<y(g!!0))$a x))(x:v)(k 0 x g)
a(r,c)=[(r+1,c+1),(r+1,c),(r+1,c-1),(r,c+1),(r,c-1),(r-1,c+1),(r-1,c),(r-1,c-1)]
m s=(y.i(/=0).concat.e[(0,0)][])(w(l+1,l+1)s(map(\_->map(\_->1)q)q))where l=y s;q=[0..2*l+2]


m is the main function, which takes a string over v^<>, and returns the appropriate integer.

### Ungolfed:

import Data.Set hiding (map, filter)

-- Generate a grid full of ones, of width and height 2x+1. We pass the length of
-- the input, and get back a grid that we could never go out of bounds from,
-- even when the input is a straight wall in any direction.
genGrid :: Int  -> [[Int]]
genGrid x = map (\_->map(\_->1) [0..2*x+2]) [0..2*x+2]

-- Update the value of a list l, such that index i now contains the value e
update :: Int -> a -> [a] -> [a]
update i e l = take i l ++ [e] ++ drop (i+1) l

-- scale update to two dimensions
set :: a -> (Int, Int) -> [[a]] -> [[a]]
set val (r,c) g = update r (update c val (g !! r)) g

-- index into a 2D array
at :: (Int, Int) -> [[a]] -> a
at (r,c) g = g !! r !! c

-- Walk the wall path. Replace any 1 we step on with a 2. Start walking from
-- given coordinates, recursively updating the spot we step on as we process
-- the input string.
walk :: (Int, Int) -> String -> [[Int]] -> [[Int]]
walk (r,c) s g = case s of
"" -> set 2 (r,c) g
'<':t -> walk (r,c-1) t (set 2 (r,c) g)
'>':t -> walk (r,c+1) t (set 2 (r,c) g)
'v':t -> walk (r+1,c) t (set 2 (r,c) g)
'^':t -> walk (r-1,c) t (set 2 (r,c) g)

-- Given an input string, generate a grid of appropriate size and walk out the
-- wall path starting at the center.
sketch :: String -> [[Int]]
sketch s = let l = length s in walk (l+1,l+1) s (genGrid l)

-- Breadth-first exploration of the 2D grid, but do not pass through walls.
-- Will touch everything that's not part of the land, and mark it as not part
-- of the land. We use a set (a list in the golfed version) to keep track
-- of which coordinates we've already explored.
explore :: [(Int, Int)] -> Set (Int, Int) -> [[Int]] -> [[Int]]
explore [] v g = g
explore (x:cs) v g
| member x v  = explore cs v g
| at x g == 2 = explore cs v g
| at x g == 0 = explore cs v g
| at x g == 1 =
explore (cs ++ (filter (\x-> notMember x v) $filtBound g$ adj x))
(insert x v) (set 0 x g)

-- Count everything marked as land to get the final total
countLand :: [[Int]] -> Int
countLand = length . filter (/=0) . concat

-- for a given list of coordinates and a 2D grid, filter those coordinates that
-- are within the grid's bounds
filtBound :: [[Int]] -> [(Int, Int)] -> [(Int, Int)]
filtBound g = filter (\(r,c) -> r >= 0 && c >= 0 && r < length g && c < length (g !! 0))

-- Given a coordinate, get all the adjacent coordinates, including diagonally
adj :: (Int, Int) -> [(Int, Int)]

-- The main function
runMain :: String -> Int
runMain = countLand . explore [(0,0)] empty . sketch

-- Print a grid (for debugging & REPL convenience)
printG :: [[Int]] -> String
printG = concat . map ('\n':) . map show


## Mathematica, 124 bytes

You probably won't be surprised to learn that Mathematica has a built-in function for measuring the area encircled by a wall. Unfortunately, it's quite bytey: ComponentMeasurements[..., "FilledCount", CornerNeighbors -> False].

With that in mind, here's my full answer. It's a function that takes a list of 1, i, -1 or -i:

1/.ComponentMeasurements[SparseArray[{Re@#,Im@#}&/@FoldList[#+#2&,2(1+I)Length@#,#]->1],"FilledCount",CornerNeighbors->1<0]&


Explanation:

• FoldList[#+#2&,2(1+I)Length@#,#] builds the wall by starting at coordinate 2(1+i)(length of wall) and successively adding the elements of the input list. (We have to start at the ridiculously large coordinate 2(1+i)(length of wall) to ensure that the wall coordinates stay positive, otherwise things break.)
• SparseArray[{Re@#,Im@#}&/@...->1] turns these coordinates from complex numbers to pairs of integers, and makes an array with 1s where the wall is and 0s elsewhere.
• 1/.ComponentMeasurements[...,"FilledCount",CornerNeighbors->1<0]& uses built-in Mathematica magic to measure the area enclosed by the wall.
• "We have to start at the ridiculously large coordinate..." good trick! Mar 19 '17 at 4:08

# PHP>=5.6.2, 888 Bytes

Online Version

<?$h=$v=0;
s($v,$h,S);
for($z=0;$z<strlen($i=$_GET[0]);){
2<($b=$i[$z++])?$h--:($b>1?$v++:($b?$h++:$v--));$e=max($h,$e);
$w=min($h,$w);$n=min($v,$n);
$s=max($v,$s); s($v,$h,X);}$f=($e-$w+1)*($s-$n+1);
ksort($a); function i($v,$h){global$a;return isset($a[$v][$h])&&$a[$v][$h]==" ";}
function s($v,$h,$l=" "){global$a;$a[$v][$h]=$l;}
function c($v,$h,$n=1){global$a;
foreach($r=range(-1,$n)as$i) foreach($r as$j) if(($i+$j)&&i($v+$i,$h+$j)){if($n)s($v,$h);return 1;}return;}
foreach($a as$v=>$z){ foreach(range($w,$e)as$h){
if(!isset($a[$v][$h])){ if(($v==$s)||($v==$n)||($h==$e)||($h==$w)||c($v,$h,0))s($v,$h); else$c[]=[$v,$h];}
}ksort($a[$v]);}
while($z){$z=0;
foreach($c as$b=>$w){if(c(...$w)){$z++;unset($c[$b]);}}}; foreach($c as$b=>$w)$a[$w[0]][$w{1}]=O; foreach($a as $k=>$v){ksort($a[$k]);$g.=join($a[$k])."\n";}echo$g;
echo $f-substr_count($g," ");

• You know you only had to output the size of the land, not the land itself right? :) Mar 18 '17 at 20:00
• @StewieGriffin this is the output ´echo \$g;` of the land. I found this approach and maybe it exists an other shorter way. If I found this way I can improve this post Mar 18 '17 at 20:13