17
\$\begingroup\$

Very related

You're given a piece of ASCII art representing a piece of land, like so:

   /‾\
__/   ‾\_
         \_/‾\
              \

Since an overline () is not ASCII, you can use a ~ or - instead.

Your challenge is to determine if it is connected by the lines of the characters. For example, the above can be traced like so:

enter image description here

To clarify the connections:

  • _ can only connect on the bottom on either side
  • (or ~ or -) can only connect on the top on either side
  • / can only connect on the top right and bottom left
  • \ can only connect on the top left and bottom right

It doesn't matter where the connections start and end as long as they go all the way across. Note that one line's bottom is the next line's top, so stuff like this is allowed:

_
 ‾

\
 \

 _
/

\
 ‾

You can assume input will only contain those characters plus spaces and newlines, and will only contain one non-space character per column.

Input can be taken as ASCII art, an array of rows, a character matrix, etc.

Testcases

Separated by double newline.

Truthy:

\
 \

\_
  \ /\/\_
   ‾     \

/\
  \/

\_
  ‾\

   /‾\
__/   ‾\_
         \_/‾\
              \

Falsy:

/
 \


//

 ‾‾
/  \

‾
 _

\____/
      /‾‾‾\
\$\endgroup\$

6 Answers 6

11
\$\begingroup\$

Python 3.8, 85 bytes

Input is taken as a list of lines, with replaced with dashes (-).

lambda L,n=0:len({(n:=n+2-ord(c:=max(x))//2%5)-x.index(c)+(c<'0')for x in zip(*L)})<2

Try it online!

\$\endgroup\$
0
7
\$\begingroup\$

J, 43 39 bytes

[:*/2=/@(I.@:*-1#.3 2&=+1&=)\' -\/'i.|:

Try it online!

  • ' -\/'i.|: Transpose and convert characters to integers.
  • 2...(...)\ For each pair of rows...
  • I.@:* Get the indices of the non-zero (non-space) element...
  • -1#.3 2&=+1&= And adjust it as follows:
    • 1&= If it's a - in either position, subtract 1.
    • 3 2&= If it's a / on the left or a \ on the right, subtract 1.
  • =/@ Are the two row indices equal after this adjustment?
  • [:*/ Are they all equal?
\$\endgroup\$
2
  • 1
    \$\begingroup\$ I think ' -\/'i.|: works for -2 \$\endgroup\$
    – ovs
    Jan 16 at 22:49
  • \$\begingroup\$ I actually just went back to this and found that as well as some other golfs... \$\endgroup\$
    – Jonah
    Jan 16 at 22:55
6
\$\begingroup\$

Retina, 77 bytes

m/(?<=^|¶)([\\_]([_\/]|.*¶ [-\\])|[\/-][-\\]| [_\/].*¶[\/-])/+`^.

^\s*\S\s*$

Try it online! Link includes test suite that takes double-spaced test cases and converts to - which the script actually uses. Explanation:

/(?<=^|¶)([\\_]([_\/]|.*¶ [-\\])|[\/-][-\\]| [_\/].*¶[\/-])/+

Repeat while the first two columns have a valid join, either a \ or _ followed by either a _ or / or a - or \ on the next line, or a / or - followed by a - or \, or a / or - followed by a _ or / on the previous line, ...

m`^.

... delete the first character of each line.

^\s*\S\s*$

Check that only one column is left.

\$\endgroup\$
5
\$\begingroup\$

Charcoal, 37 bytes

WS⊞υι⬤Φθκ¬↨EE²⭆υ§ν⁺κλ⁺⌕λ⌈λ№⁺_§\/μ⌈λ±¹

Try it online! Link is to verbose version of code. Takes input as a rectangle of newline-terminated lines and outputs a Charcoal boolean, i.e. '-' for continuous, nothing if not. Assumes that any character not one of \_/ or space is the overline character. Explanation:

WS⊞υι

Input the land.

⬤Φθκ

Loop over all the columns except one.

E²⭆υ§ν⁺κλ

Get the current and the next column.

E...⁺⌕λ⌈λ№⁺_§\/μ⌈λ

Find the positions of the non-space characters, but add 1 if the character is a _, or if it is either \ or / depending on the column.

¬↨...±¹

Ensure the positions are equal.

\$\endgroup\$
3
\$\begingroup\$

BQN, 35 bytesSBCS

Uses ¯ MACRON instead of the overline.

⌈˝{≡´¯1‿1↓¨((/˘𝕨=𝕩)-𝕨∊'¯'∾⊢)¨"/\"}⍉

Run online!

┌─            
╵"\_                     # Input character matrix    - m
    \ /\/\_   
     ¯     \" 
             ┘
"\_\¯/\/\_\"             # collapsed - ⌈˝m
⟨ 0 0 1 2 1 1 1 1 1 2 ⟩  # the depth of each column  - /˘(⌈˝m)=(⍉m)

⟨ 0 0 0 1 1 0 1 0 0 0 ⟩  # locations of '¯' and '/'  - (⌈˝m)∊'¯'∾'/'
⟨ 0 0 1 1 0 1 0 1 1 2 ⟩  # depth - ^locations
⟨ 0 0 1 1 0 1 0 1 1 ⟩    # the last element dropped  - ¯1↓...

⟨  1 0 1 1 0 1 0 1 0 1 ⟩ # locations of '¯' and '\'  - (⌈˝m)∊'¯'∾'\'
⟨ ¯1 0 0 1 1 0 1 0 1 1 ⟩ # depth - ^locations
   ⟨ 0 0 1 1 0 1 0 1 1 ⟩ # the first element dropped - 1↓...
\$\endgroup\$
2
  • \$\begingroup\$ Nice. How do you like BQN compared with APL so far? \$\endgroup\$
    – Jonah
    Jan 17 at 19:48
  • 1
    \$\begingroup\$ @Jonah for golfing purposes it is quite nice (as long as you don't need base conversion). Nothing · (similar to J's cap [:) makes writing tacit functions easier and there are a few additional useful operators, like Catch, Under, Before, etc. The somewhat different selection of primitives for sequences takes a while to get used to, but Occurence Count has already provided me with multiple very short answers in return ;). For non golfed code I tend to notice that Dyalog actually did a decent job at making APL run fast, there is definitely a gap there. \$\endgroup\$
    – ovs
    Jan 17 at 21:13
1
\$\begingroup\$

Jelly, 28 bytes

“-\/_”iⱮⱮZTḢ+S+3BḊƲƲ€FḊs2E€Ạ

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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