5
0
In a matrix of characters, a cursor is a movable position between two adjacent characters, before the first character or after the last character in a line, like that "I"-shaped indicator which moves while you type.
In this challenge, a cursor position is considered valid if at least one of its two sides touches a space. The cursor travels through valid positions in a matrix, by moving up, down, left or right by one position each time. It could also move left or right though the line separator, to the end of the previous line, or the beginning of the next line respectively.
Given a character matrix as input, and a cursor initially at the top left corner, your task is to determine whether it can reach the bottom line.
This is a code-golf and decision-problem problem; the rules of this question is the same as what these two tags specified.
Rules
- You can assume that the input consists only of
.
, - You may require there must or must not be a trailing newline in the input, if you want to.
- Your code should output either a truthy value (if the condition is true), or a falsy value (if the condition is false).
Examples
Example input(where ░
is the space character):
░.....░
░......
.░.░.░.
.░░░░░░
......░
The cursor can reach the bottom in this case. This process of moving the cursor will work: down, right(touches spaces on the left), down(touches spaces on the right), down, right(touches spaces on both sides) 6 times, and down(touching spaces on the left).
This will also work(░
still represent spaces):
░░
.░
.░
The cursor starts at the up-left corner. After moving right two times, it can move down twice, which touches the bottom of the lines.
This example will not work(░
represent spaces again):
░...
...░
The cursor cannot move down, as there is no sufficient whitespace to be touched. There are only newline characters on the right of the input, and there are no newline characters on the left(otherwise a line would have spanned two lines.)
The cursor can also move up and left, in order to solve a maze-like input as in this one:
░....
░░░░.
...░.
.░░░.
░....
░░░░░
In this case, the cursor has to move left in order to get through spaces and get to the bottom line.
Here is another important test case: the cursor can move left or right to another line.
░░░░
...░
░...
In this case, the cursor can reach the last line, as the cursor can move left or right in order to get to the next line. Moving the cursor down is blocked on the right position of row 2, column 4; however, the cursor can still get to the last line if it moves right (touching spaces).
This is a fairly interesting test input:
░░..░
░...░
....░
In this case, the cursor can reach the last line, by moving down and left to the end of the first line, then down twice to reach the end of the bottom line.
2Can we only go down and towards the right as the test cases seem to reflect? Or is going back up, or towards the left possible as well in finding our path? But in general the challenge is to determine if a path from top to bottom is possible, pretending the newlines are an additional space at the right-hand side? – Kevin Cruijssen – 2019-07-05T07:33:01.920
2
Ahh, now I finally understand it! My first comment was incorrect. This isn't a path-finding with the new-line on the right active as additional cell. It's path-finding while going over the borders of the matrix instead of inside the character-cells themselves. So in the first test case, this would be the path. Now it makes sense, including how the newline works. Should have paid closer attention to the word 'cursor'..
– Kevin Cruijssen – 2019-07-05T13:59:58.6431Maybe it should be clearer somehow that we travel over the border of the matrices (like a cursor). Based on the downvotes and close-votes of others I can imagine I'm not the only one who was confused by the wording of the example and test cases. – Kevin Cruijssen – 2019-07-05T14:03:17.553
1In your latest edit, does it mean there must not be a trailing newline on the last line in your original spec? If yes, I'd suggest changing it as it appears too random, but if you insist, please edit that into the specification, not only as an example. – jimmy23013 – 2019-07-07T03:55:13.437
I might have mis-understood your specification "it cannot move left or right through a line separator to another line". Can you explain it? (I thought you meant that the cursor cannot go to the next line via moving right on the end of a line.) If I did, I will delete that test input. – None – 2019-07-07T03:57:41.167