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.
. / \ \ / / \ \/ \ \ /\ \ /\ \ \ /
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:
Note that columns are 1-indexed, mostly for consistency with text editor conventions.
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
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
\ 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.
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.)
Note that the input here is exactly one byte. This is the smallest case you should be able to handle.
. \ \ \ \
Note that there are no spaces after these slashes.
. / /\\ / \ //\ \/// // \\/ \/\ /\/
. / / / \\\ /\\ / \
. \ / / \
. \ \/\/\/
(anything but a nonnegative number)
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!