95
\$\begingroup\$

Write a program that takes a single line string that you can assume will only contain the characters /\_‾. (That's forward and backward slash, underline and overline. You can use ~ in place of overline if you need since overline is not convenient ASCII.)

For example, one possible input is:

__/‾‾\/\_/‾

Your program needs to output a truthy or falsy value depending on whether the left edge of the string is "connected", so to speak, to the right edge of the string via the lines of the characters. So, if the kerning was a bit less, there would be a solid black (albeit kinky) line all the way from the left edge to the right, like an unbroken piece of string or twine.

The output for the above example would be true because the edges are connected:

example path

To be clear on the connections:

  • / connects on its bottom left and top right
  • \ connects on its top left and bottom right
  • _ connects on its bottom left and bottom right
  • (or ~) connects on its top left and top right

Also:

  • It doesn't matter whether the edges of the string started on the top or the bottom, it only matters that they connect horizontally through the whole length of the string.

  • You can assume the input string is non-empty, and of course just one line.

Here are some more examples followed by 1 (truthy) if they are connected or 0 (falsy) if not:

__/‾‾\/\_/‾
1

_
1

\
1

/
1

‾
1

___
1

\/
1

/\/
1

/\/\
1

‾‾‾
1

\\
0

‾‾
1

_‾
0

‾_
0

\_____/
1

\/\\/\\___
0

\/\__/‾‾\
1

______/\_____
1

‾‾‾‾‾‾\\_____
0

‾‾‾‾‾‾\______
1

_____/‾‾‾‾‾
1

\___/‾‾‾\___/‾‾‾
1

\_/_\_
0

\_/\_
1

/\/\/\/\/\/\/\/\/\/\/\/
1

____________________
1

‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
1

‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾/
0

‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾\
1

/\‾/\‾___/\_\/__\/\‾‾
0

The shortest code is the winner.

\$\endgroup\$
9
  • 38
    \$\begingroup\$ Welcome to PPCG! Nice first challenge. \$\endgroup\$ Mar 18 '19 at 15:30
  • 1
    \$\begingroup\$ Are the characters specified in your challenge the only ones that will appear in the string? \$\endgroup\$
    – Gymhgy
    Mar 18 '19 at 15:31
  • \$\begingroup\$ @EmbodimentofIgnorance Yes, only the 4. \$\endgroup\$ Mar 18 '19 at 15:34
  • 33
    \$\begingroup\$ Wait you could make a language out of this \$\endgroup\$
    – Delioth
    Mar 18 '19 at 18:57
  • 2
    \$\begingroup\$ @Arnauld No, I really think only truthy for connected and falsy for unconnected. (Unless allowing a swap is normal for this kind of question?) \$\endgroup\$ Mar 19 '19 at 4:35

34 Answers 34

1
2
0
\$\begingroup\$

APL+WIN, 58 bytes

m←2 2⊤'_/\~'⍳s←,⎕⋄(1+⍴s)=+/((↑m[0;]),m[1;])=m[0;],¯1↑m[1;]

Prompts for input of string, index origin 0 and uses ~ for upper character

Try it online! Courtesy of Dyalog Classic

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

TSQL code, 90 bytes

declare @ varchar(99)='__/‾‾\/\_/‾'

while len(@)>1set
@=iif(charindex(left(@,2),'__/\/‾‾\_')=0,'',stuff(@,1,1,''))print
len(@)

Try it out

TSQL query, 119 bytes

use master
declare @ varchar(99)='/\/\_/‾\'

SELECT top 1sign(charindex(substring(@,number,2),'__/\/‾‾\_'))FROM
spt_values 
WHERE number<len(@)and'P'=type
ORDER BY 1

Try it out

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

Scala, 75 bytes

def f(s:String)="""[^/\\_~]|[/~][/_]|[\\_][\\~]""".r.findFirstIn(s).isEmpty
\$\endgroup\$
0
\$\begingroup\$

PHP, 67 bytes

for(;$s=$argn[$i++];)$f|=$l%3<1==($l=ord($s)%18%4)%2&&$i>1;echo!$f;

Try it online!

Created a non RegEx solution without looking at other solutions. Uses ~ character.

Converts each input character to a number from 0 to 3 using its ASCII code and mod 18 and mod 4:

  • ~ (126) % 18 % 4 = 0
  • _ (95) % 18 % 4 = 1
  • \ (92) % 18 % 4 = 2
  • / (47) % 18 % 4 = 3.

Loops on each character and current character's converted ASCII mod 2 is compared with previous character's converted ASCII mod 3 < 1 and they shouldn't be equal for all characters so it can be a valid string.

~ | Converted ASCII (CA) = 0 | CA % 3 < 1 = 1 | CA % 2 = 0
_ | Converted ASCII (CA) = 1 | CA % 3 < 1 = 0 | CA % 2 = 1
\ | Converted ASCII (CA) = 2 | CA % 3 < 1 = 0 | CA % 2 = 0
/ | Converted ASCII (CA) = 3 | CA % 3 < 1 = 1 | CA % 2 = 1

Since CA % 3 < 1 (previous character) and CA % 2 (current character) shouldn't be equal, only valid combinations will be these: ~~, ~\, __, _/, \_, \/, /~, /\ and if a string has a combination other than those, it will be invalid.

\$\endgroup\$
1
2

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.