Fill in the path

7

2

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.

Arcturus

Posted 2015-11-16T15:33:23.110

Reputation: 6 537

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? – Arcturus – 2015-11-16T15:45:15.880

Fishing? do you mean ><>?

– Aaron – 2015-11-16T15:53:03.413

@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.

– Arcturus – 2015-11-16T15:56:52.737

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. – Aaron – 2015-11-16T16:10:15.900

@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. – Arcturus – 2015-11-16T16:14:55.213

The rule about "immediate termination" seems overly constraining. What's wrong with implementations which handle loops by only iterating at most once per character in the bounding box? – Peter Taylor – 2015-11-16T19:30:19.850

@PeterTaylor Because the path the pointer would be on at that point is closed, the challenge above would run into an infinite loop. If you are talking about my idea of a programming language, I'd suggest we move the conversation to chat so this question doesn't get overflowed. (Though yes, running multiple loops of the same circuit would be a thing in Brunswick Stew). – Arcturus – 2015-11-16T23:28:44.460

I'm talking about the sentence "the following rule must be implemented: if the pointer reaches a directional character more than one time, the program must immediately terminate". That's anti-natural for some languages, which would prefer to precalculate an upper bound on the number of steps (for input of size w by h, at most w*h steps can be taken without looping) and run their main loop that many times. In my opinion the question would be better if it simply required that programs terminate in a reasonable time and didn't try to dictate the looping structures used. – Peter Taylor – 2015-11-16T23:34:54.920

Will each line of the input be padded with spaces to make the line length equal to the grid width? If not is the grid width assumed to be the length of the longest line? Is the output allowed to be padded with spaces? I ask because the sample is not padded with spaces and the | at the end of the second line moves out by one in the output (probably a mistake) which makes me think maybe it just wasn't padded because it was made by hand. – user81655 – 2015-11-16T23:57:54.357

In the simple input/output above '/' seem to mean down/left, but in the specification it means up/right. Is there a mistake here or have I misunderstood something? – Emigna – 2015-11-17T07:41:19.023

@Emigna Sorry about that. It's fixed. – Arcturus – 2015-11-17T14:36:18.633

Answers

1

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 -&).

Jonathan Frech

Posted 2015-11-16T15:33:23.110

Reputation: 6 681