7
\$\begingroup\$

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.

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

3 Answers 3

6
\$\begingroup\$

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

\$\endgroup\$
6
\$\begingroup\$

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.

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

Retina, 48 bytes

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

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

\$\endgroup\$

Your Answer

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

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