# Where will the ball land?

Given a string where the first line contains spaces and one period (., the "ball"), followed by lines containing spaces, forward slashes (/), and backslashes (\), determine what column the ball will land in after falling from its starting position. Each / moves it to the left by 1 column and each \ moves it to the right by 1 column.

### Sample input

    .
/   \  \
/   /
\   \/  \
\   /\
\ /\  \
\    /


### Sample output

The ball starts in column 5, hits the / on line 3, then the three \'s on lines 5 through 7 for a final position of:

7


Note that columns are 1-indexed, mostly for consistency with text editor conventions.

### Edge cases

If the ball hits a / in the first column, it's eternally stuck in the nonexistent column 0. Your program should handle this correctly by printing 0.

If the ball hits either side of a \/ pattern, the result is undefined. Your program is permitted to terminate with no output, loop infinitely, or print an error message (my solution prints -1), but it must not print anything that can be perceived to be valid output.

If the ball hits the left slash in a \\ pattern, it should end up directly beneath the right slash, not to the right of it. The solution I originally envisioned was prone to getting this wrong, so don't go down that path!

There may or may not be spaces after the . or the last / or \ on each line. Your program should not rely on such padding being available. On a similar note, there may or may not be any lines following the first line.

You may assume that the first line will have zero or more spaces and exactly one .. Subsequent lines, if any, will have zero or more spaces and zero or more slashes.

### Implementation details

Your program must output a single number to standard output. (Yes, a trailing newline is fine. Yes, the number may have more than one digit.)

### Test cases

Input:

.


Output:

1


Note that the input here is exactly one byte. This is the smallest case you should be able to handle.

Input:

 .
\
\
\
\


Output:

 6


Note that there are no spaces after these slashes.

Input:

  .
/
/\\  /  \
//\ \/// //
\\/ \/\ /\/


Output:

0


Input:

  .
/ / /
\\\
/\\
/   \


Output:

1


Input:

   .

\
/
/

\


Output:

4


Input:

 .
\

\/\/\/


Output:

(anything but a nonnegative number)


### Closing remarks

This question is similar to Simulate a (gravity-based) billiard-ball-type computer, but significantly simpler, so hopefully it will gain more interest.

I have a 169-character solution in Python. I'm sure the talented golfers here can tear that record to pieces, though. :^)

This is , so the shortest answer in characters will be accepted at the end of the month!

• It's also very similar to A Mere Bagatelle with a slightly different import format and only one throw. You can borrow and modify my test scripts if you want. – Gareth Apr 20 '14 at 6:52
• Well, shoot, the title of that question wasn't suspicious enough for me to check it. Sorry about that. – Fraxtil Apr 20 '14 at 7:08
• It's okay, that question was two and a half years ago. – Gareth Apr 20 '14 at 7:10
• I suggest that in the last example, the output should be "The ball is stuck". – Mukul Kumar Apr 24 '14 at 16:30
• Does it count as the end of the month yet >.< – alexander-brett May 1 '14 at 17:46

# Python, 143B

import sys
for l in sys.stdin:
a=l.find('.')
if a>-1:F=a
elif F>-1:
if'\\/'in l[F-1:F+2]:z
F+={'\\':1,'/':-1}.get((l+' '*F)[F],0)
print F+1


Using the space/tab indentation trick. I haven't done anything particularly clever here. F is the current index, l is the current line; z is undefined so it throws an exception, which is definitely not a positive integer, handling the \/ situation.

## CJam, 61 bytes

qN/('.#)\_:,0+:e>f{' e]" /"f#0\+}{1$1$=\@2$-_@=@[\]$[W1]#/z}/


If the rule regarding \/ is lifted (and we are not required to handle it), this can be shortened to 41 bytes:

qN/('.#)\_:,:e>f{' e]" /"f#0\+0+}{1\$=-}/


# Java 8, 213 bytes

s->{String[]a=s.split("\n");int r=a[0].indexOf('.'),c;for(String x:a){for(;r>x.length()-2;x+=" ");c=x.charAt(r);if(c==46)continue;if(c>47&x.charAt(r+1)==47)return-1;r+=c<33?0:c<48?-1:1;if(r<0)return 0;}return++r;}


Returns -1 when we are stuck inside a \/.

Explanation:

Try it here.

s->{                        // Method with String parameter and integer return-type
String[]a=s.split("\n");  //  Split the input on new-lines
int r=a[0].indexOf('.'),  //  Get the index of the dot on the first line
c;                    //  Temp integer
for(String x:a){          //  Loop over the lines of the input
for(;r>x.length()-2;x+=" ");
//   Append trailing spaces if necessary
c=x.charAt(r);          //   Get the character at the current index of this line
if(c==46)               //   If this is the first line (the char is the dot)
continue;             //    Continue to the next iteration of the loop
if(c>47&x.charAt(r+1)==47)
//   If we're stuck in a \/
return-1;             //    Return -1
r+=c<33?                //   If the current character is a space:
0                   //    r remains at the same index
:c<48?               //   Else if it's a /:
-1                  //    Index r is decreased by 1
:                    //   Else (if it's a \):
1;                  //    Index r is increased by 1
if(r<0)                 //   If r is now -1:
return 0;             //    Return 0
}                         //  End of loop
return++r;                //  Return the result 0-indexed index + 1
}                           // End of method