Write a program that fills in the paths of a grid of characters (ASCII 20-7E) according to the following rules.
The path is governed by eight directional characters: -
, |
, /
, \
, &
, @
, %
, and *
, as specified in the table below. The direction of the path will change when it reaches one of the eight above characters. Note that the direction the pointer is moving will always be on a line, forming an angle to the x-axis with radian measure kπ/4
for integer k
.
- Move right
| Move down
/ Move up/right
\ Move down/right
& Move left
@ Move up
% Move down/left
* Move up/left
The program starts at the top left of the grid, which is either -
, \
, or |
and the pointer will move in the direction specified.
The objective of the program is to draw the path that the program takes by filling in whitespace according to the direction that the program is traveling. Any whitespace the pointer encounters is filled in, but if the pointer encounters a character not described above, the character is ignored (or put on the stack, if you choose to implement in the bonus).
The program ends when the pointer reaches the edge of the initial "box" the grid was in without hitting another directional character. This initial box does not contain any blank lines on the perimeter of the grid.
It should be noted that a specific point on the grid may change between the beginning and end of the program. For example, if the initial grid is - &
, then by the time the pointer reaches &
, the grid will be --&
. Then the pointer will move one to the left, encounter -
, and move to the right. It will not encounter an empty space, as the original grid suggests.
One of the things that might happen with this program is the appearance of infinite loops. Trivially, -&
would be an infinite loop if the program were allowed to run indefinitely.` To counter this, your program must terminate within a reasonable amount of time.
The input to the program is a set of characters with a -
, \
, or |
as the first character in the first line. There cannot be any trailing or leading newlines.
Sample input
- h %
| &( &f |
#
r @ )
5
Sample output
-----h-----%
|&&&(&&&&f% |
|# @%
| r @ )
5
Bonus
If your program prints the non-directional characters the pointer encounters separated from the modified grid by a newline, multiply your byte count by 0.9.
This is code golf, so standard rules apply. Shortest code in bytes wins.
A,B,...,H
for directions, even though the use of-
for subtraction can be avoided by usingd
like in Fishing. I'll give it some thought. \$\endgroup\$