17
4
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 may read from a file (specified as a command-line argument) or read from standard input, at your convenience.
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 code-golf, 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 – 2014-04-20T06:52:33.037Well, shoot, the title of that question wasn't suspicious enough for me to check it. Sorry about that. – Fraxtil – 2014-04-20T07:08:18.267
It's okay, that question was two and a half years ago. – Gareth – 2014-04-20T07:10:36.873
I suggest that in the last example, the output should be "The ball is stuck". – Mukul Kumar – 2014-04-24T16:30:50.273
Does it count as the end of the month yet >.< – alexander-brett – 2014-05-01T17:46:57.257
Indeed it does. – Fraxtil – 2014-05-01T22:18:16.497