Identify the direction of lines in an ASCII-figure

7

2

Challenge:

Take a rectangular figure consisting of the two characters # and (whitespace, ASCII-32), and identify which direction the lines are. The options are: 'Vertical', 'Horizontal', 'Left Diagonal' and 'Right Diagonal'.

Input:

The input will be a figure of size n-by-m where 5 <= m,n <= 20. There will be two spaces between horizontal lines, two spaces between lines in the horizontal/vertical direction. Note that the lines doesn't have to start or end in a corner. The input will look like this:

Horizontal:

##########


##########


##########

Vertical (note that there can be leading spaces):

 #  #  #
 #  #  #
 #  #  #
 #  #  #
 #  #  #

Left diagonal:

#  #  #
 #  # 
  #  # 
#  #  #
 #  #  

Right diagonal:

#  #  #  #  # 
  #  #  #  #  
 #  #  #  #  #
#  #  #  #  # 
  #  #  #  #  

  • The input format is optional. You may substitute the visual newlines by \n, or create the grid by concatenating the rows with ASCII-10 (relevant for MATLAB/Octave).
  • You may not take the input as numbers (1/0) instead of # and 0.

Output:

The output shall be 4 distinct values/outputs for each of the four different cases. It can for instance be 1,2,3,4, or V,H,L,R.

Test cases:

There will be an asterisk over the top left corner of the figure, and an asterisk below the bottom left corner of the figures. This is to indicate where the figure starts and ends.

Horizontal:
*
#####


#####

    *
*


###########


          *
*

##########################


##########################


##########################
                         *

Vertical:
*
#  #  #  #
#  #  #  #
#  #  #  #
#  #  #  #
#  #  #  #
         *
*
 #  #  #  #  #  #  
 #  #  #  #  #  #  
 #  #  #  #  #  #  
 #  #  #  #  #  #  
 #  #  #  #  #  #  
                  *

Left diagonal
*
#  #  #
 #  #  
  #  # 
#  #  #
 #  #  
      *
*
#  #  #  #  #
 #  #  #  #  
  #  #  #  # 
#  #  #  #  #
 #  #  #  #  
  #  #  #  # 
            *
*
  #  #  # 
#  #  #  #
 #  #  #  
  #  #  # 
#  #  #  #
         *

Right diagonal
*
#  #  #  #  # 
  #  #  #  #  
 #  #  #  #  #
#  #  #  #  # 
  #  #  #  #  
             *
*
 #  #  #  #  #  #  #
#  #  #  #  #  #  # 
  #  #  #  #  #  #  
 #  #  #  #  #  #  #
#  #  #  #  #  #  # 
                   *

This is so the shortest code in each language wins.

Stewie Griffin

Posted 2017-07-02T11:32:17.047

Reputation: 43 471

Can we assume that the input is padded to a rectangle with spaces? – Martin Ender – 2017-07-02T12:15:23.697

Actually, the spec says the input will be rectangular, but many of the examples aren't. – Martin Ender – 2017-07-02T12:17:44.440

2I really want a Mathematica answer to show up using something like MaxDetect@*Radon@*Blur@*Graphics@*Text, but I can't imagine it'd be at all competitive – Julian Wolf – 2017-07-02T18:44:41.487

Answers

6

Slip, 6 + 2 = 8 bytes

+2 bytes for the f flag.

^*`#`#

Try it here!

Prints one of the following:

##

#
#

# 
 #

 #
# 

Explanation

The ^* sets the IP's direction to any one of the eight octilinear directions. Then `#`# simply matches two hash symbols in that direction. The f flag limits Slip to looking only for the first match, and by default the bounding box of that match is printed to STDOUT.

The pattern itself would be two bytes shorter in Snails (z\#2), but I don't think Snails can print the match itself, it only has numeric output (and adapting the pattern to printing 4 different numbers based on the direction would most likely take a lot more bytes).

Martin Ender

Posted 2017-07-02T11:32:17.047

Reputation: 184 808

6

Grime, 4 bytes

s./$

Try it online!

Prints one of the following 2x2 blocks (which should be padded with spaces, some of which are swallowed up by SE):

##

# 
# 
# 
 #
# 

Where the last one indicates lines along antidiagonals.

Explanation

The pattern matches a 2x2 square starting from the first # in reading order. The # is matched with s (the "symbol" character class), the following character with . (which can be any character). To this 2x1 rectangle we concatenate another one vertically with /. That other rectangle is just $ which always matches, so this simply fills up the 2x2 square with the two characters below. This unique identifies all four cases.

Martin Ender

Posted 2017-07-02T11:32:17.047

Reputation: 184 808

1I want to note that this answer only works because of a bug in the Grime interpreter. :P – Zgarb – 2017-07-02T19:02:17.223

@Zgarb That's just fair for the other bug that doesn't let me compete with Snails in this challenge. ;)

– Martin Ender – 2017-07-02T19:03:20.500

1

Retina, 48 bytes

ms`^(.*¶)?( *)( ?#)((#)|[^¶]*¶\2( ?#)).*
$3$5¶$6

Try it online! Returns the same results as @MartinEnder's Slip answer.

Neil

Posted 2017-07-02T11:32:17.047

Reputation: 95 035