8
\$\begingroup\$

Caves and Cliffs edition

Part 1, Very related

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

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

But unlike in the previous challenge, the terrain is mountainous, with caves and cliffs, and can turn back on itself. Also, a new character is introduced, |, which can connect to any of its four corners.

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

Your challenge is still to determine if it is connected by the lines of the characters such that a single line connects them all without backtracking or crossing itself. 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
  • | can connect on all four corners, but not to anything that connects to more than one corner. | must have a single connection above itself and a single connection below.

That one's a bit confusing, so the following are legal:

|
 \

|
\

 _
|_

‾|
/

And the following aren't:

|
‾
_
|

||

It doesn't matter where the connections start and end as long as they're all connected. 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 contain at least 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\$
7
  • \$\begingroup\$ So self-intersections, nested regions, etc, are all allowed? So long as there is a path from each cell to every other cell? For example: Try it online! \$\endgroup\$
    – Jonah
    Jan 22, 2022 at 19:12
  • \$\begingroup\$ Hmm... based on some of the falsy examples that must be falsy. is the rule that your single connected line tracing it is not allowed to backtrack? \$\endgroup\$
    – Jonah
    Jan 22, 2022 at 19:14
  • \$\begingroup\$ @Jonah No self-intersections, yes the line can't backtrack. \$\endgroup\$
    – emanresu A
    Jan 22, 2022 at 19:18
  • \$\begingroup\$ What if there is an input that has a continuous line and a disconnected (but continuous) loop? Is it continuous? \$\endgroup\$
    – AnttiP
    Jan 23, 2022 at 14:08
  • \$\begingroup\$ @AnttiP Example? \$\endgroup\$
    – emanresu A
    Jan 23, 2022 at 19:13

1 Answer 1

4
\$\begingroup\$

Charcoal, 190 bytes

WS⊞υιFυ«⸿Fι«M³→F№\|/κP|@#≡κ_P^²‾PV²/P/²\P\²»⸿⸿»J⁰¦⁰Fυ«⸿Fι«M³→≡κ|F⁸¿‹ ⊟KD³✳λP✳λ²»⸿⸿»UMKA⎇⁼#ιψι≔⟦⟧θF׳LυF׳⊕⌈EυLκ«Jκι≔LΦKM‹ λη¿∧‹ KK›²η⊞θ⟦ικη⟧»¿∨⁻²Lθ⊙θ⊖⊟ι⎚«≔⊟θηJ⊟η⊟ηW⌕AEKM‹ κ¹✳⁻³⊟ιψ¿⊖LKA⎚«⎚¹

Try it online! Link is to verbose version of code. Takes input as a list of newline-terminated strings and outputs a Charcoal boolean, i.e. - for continuous, nothing if not. Explanation:

WS⊞υι

Input the terrain.

Fυ«

Loop over the rows of the terrain.

⸿

Start each row on a new line. This is because we'll be drawing above the current line, so we want the actual drawing to start at row 0.

Fι«

Loop over each character of the row.

M³→

Space the characters 3 apart. (This should be one before and two after, but unfortunately Switch makes doing that less golfy.)

F№\|/κ

If the current character is a \, | or /, then...

P|@#

... output a marker in case the character above or below is a |.

≡κ

Switch on the current character.

_P^²

If it's a _ then write to the centre and the bottom left and bottom right corners of this 3×3 square.

‾PV²

If it's a then write to the centre and the top left and top right corners.

/P/²

If it's a / then draw from the bottom left to the top right.

\P\²

If it's a \ then draw from the top left to the bottom right.

»⸿⸿

Add another two rows so that each row is spaced 3 apart vertically as well.

»J⁰¦⁰

Jump back to the beginning.

Fυ«⸿Fι«M³→

Loop through the rows and columns as before.

≡κ|

This time, only process | characters.

F⁸

Check all 8 directions. (Actually directions 0 and 4 are useless, but it's easier to include them.)

¿‹ ⊟KD³✳λ

See if there's a \, | or / in that direction.

P✳λ²

If so then draw a connection to it.

»⸿⸿

Finish processing each row.

»UMKA⎇⁼#ιψι

Remove any unused #s.

≔⟦⟧θ

Start collecting points that do not have two neighbours.

F׳LυF׳⊕⌈EυLκ«Jκι

Loop over each cell of the canvas, taking into account that it's offset by 2 characters to the right.

≔LΦKM‹ λη

Get the number of neighbours of this cell.

¿∧‹ KK›²η

If this cell is not empty and does not have two neighbours, then...

⊞θ⟦ικη⟧

... add it to the list.

»¿∨⁻²Lθ⊙θ⊖⊟ι

If there aren't exactly two points with exactly one neighbour each, then...

⎚«

... simply clear the canvas, otherwise:

≔⊟θηJ⊟η⊟η

Jump to one of the two ends.

W⌕AEKM‹ κ¹

While there is an adjacent character...

✳⁻³⊟ιψ

... erase the current character and move in that direction.

¿⊖LKA

If there is more than one character left, then...

... simply clear the canvas, otherwise...

«⎚¹

... clear the canvas and output success.

\$\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.