7
\$\begingroup\$

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.

\$\endgroup\$
11
  • \$\begingroup\$ This challenge actually sounds like a description of a programming language (I'm reminded of Fishing somewhat). Maybe I'll look into how to make one... What's generally required to make a golfing language? \$\endgroup\$
    – Arcturus
    Commented Nov 16, 2015 at 15:45
  • \$\begingroup\$ Fishing? do you mean ><>? \$\endgroup\$
    – Aaron
    Commented Nov 16, 2015 at 15:53
  • \$\begingroup\$ @Aaron No, Fishing, because the language would probably have most of the same functions and is 2D. It's the 2D language I'm most familiar with. \$\endgroup\$
    – Arcturus
    Commented Nov 16, 2015 at 15:56
  • \$\begingroup\$ Woops, I should have searched the wiki before talking. Well I'll at least take the opportunity to add that if you make it into a language you might end up annoyed by the need of other symbols for division and substraction. \$\endgroup\$
    – Aaron
    Commented Nov 16, 2015 at 16:10
  • \$\begingroup\$ @Aaron It would probably be wiser to use A,B,...,H for directions, even though the use of - for subtraction can be avoided by using d like in Fishing. I'll give it some thought. \$\endgroup\$
    – Arcturus
    Commented Nov 16, 2015 at 16:14

1 Answer 1

1
\$\begingroup\$

Python 2, 347*.9 = 312.3 bytes

def R(I):
 i=x=y=F=V=0;h=len(I);w=max([len(j)for j in I]);I,m,n,E,e,C=[[k for k in j.ljust(w)]for j in I],-1,"","-|/\\&@%*","10011m11m00mm1mm",""
 while 0<=x<w!=0<=y<h!=len(I)**4>i:
	if I[y][x]==" ":I[y][x]=C
	c=I[y][x]
	if c in E:H=2*E.index(c);exec"F,V="+e[H]+","+e[H+1];C=c
	elif c!=" ":n+=c
	x+=F;y+=V;i+=1
 for i in I:print"".join(i)
 print n

Try it online!

To comply with standard rules, a function is defined. The function's input is a list of strings representing the input (see the tio.run footer). Non-directional characters are printed, program terminates after a lot of steps (to halt on inputs like -&).

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.