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.

  • Can we assume that the input is padded to a rectangle with spaces? – Martin Ender Jul 2 '17 at 12:15
  • Actually, the spec says the input will be rectangular, but many of the examples aren't. – Martin Ender Jul 2 '17 at 12:17
  • 2
    I 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 Jul 2 '17 at 18:44

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

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.

  • 1
    I want to note that this answer only works because of a bug in the Grime interpreter. :P – Zgarb Jul 2 '17 at 19:02
  • @Zgarb That's just fair for the other bug that doesn't let me compete with Snails in this challenge. ;) – Martin Ender Jul 2 '17 at 19:03

Retina, 48 bytes

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

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

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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