A stretchy snake looks something like this:
<||=|||:)~
Each separate sequence of vertical bars (|
) in a stretchy snake, known as a stretchy portion, is individually extendable to twice its width, and is drawn with alternating slashes (/
,\
) once extended.
The particular snake above has two such stretchy portions, giving it four possible poses:
<||=|||:)~
</\/\=|||:)~
<||=/\/\/\:)~
</\/\=/\/\/\:)~
The general form of a stretchy snake in its least stretched pose is defined by this regex:
<(\|+=)*\|+:\)~
Which can be stated in words as:
<
, followed by any number of sequences of|
's joined with=
signs, followed by:)~
.
So <|:)~
and <||:)~
and <|=|:)~
and <|=|=||=|||||=||:)~
are stretchy snakes, but <=:)~
and <=|:)~
and <||=:)~
and <|==||:)~
are not.
Stretchy snakes can also face left instead of right, e.g. ~(:|||=||>
. The forms are the same, just mirrored.
Challenge
Write a program that takes in a single line string of two stretchy snakes facing each other, with some number of spaces in between. Both snakes will be in their least stretched pose (all vertical bars, no slashes). The string will start with the tail of the right-facing snake and end with the tail of the left-facing snake (you may optionally assume there's also a trailing newline).
For example, here's a possible input with five spaces between the snakes:
<|=||:)~.....~(:||||>
I'm using periods (.
) instead of actual space characters for clarity.
Zero spaces between snakes is also valid input:
<|=||:)~~(:||||>
We say the snakes are kissing when their tongues are touching like this.
Your program needs to extend some combination of the stretchy portions of both of the snakes such that the snakes have the fewest number of spaces possible between them (without overlapping), i.e. such that the snakes are as close to kissing as possible.
Both the snakes' tails are fixed but their heads and bodies can move - right for the right-facing snake, left for the left-facing snake - according to what stretchy portions have been extended.
The output of your program is the single line string (plus optional trailing newline) that shows the snakes as close to kissing as possible, with alternating slashes drawn in place of vertical bars for stretchy portions that have been extended.
For example, the output for <|=||:)~.....~(:||||>
(from above) would be:
</\=||:)~~(:/\/\/\/\>
This is the only solution here because with any other combination of the stretchy portions extended, the snakes would either overlap or be farther away from kissing.
If there are multiple solutions possible, the output may be any one of them.
For example, if the input were
<|=||:)~.....~(:|||=|>
the output could be
<|=/\/\:)~~(:/\/\/\=|>
or
</\=||:)~~(:/\/\/\=/\>
Remember that it won't always be possible to make the snakes kiss, but you still need to get them as close as possible.
For example, if the input were
<||=||||:)~...~(:||>
the output could be
</\/\=||||:)~.~(:||>
or
<||=||||:)~.~(:/\/\>
If the snakes are already kissing, the output will be the same as the input. e.g.
<|=||:)~~(:||||>
In general, the output will be the same as the input if the extension of any stretchy portion would make the snakes overlap. e.g.
<|||=|||:)~..~(:||||=|||||=||||||>
Notes
- Takes input from stdin or the command line as usual, or write a function that takes a string. Print or return the output.
- You can use periods (
.
) in the input and output in place of spaces ( - It's only important that slashes alternate within the sequence of vertical bars they replaced. Their ordering in the snake at large or whether a forward or backward slash comes first doesn't matter.
- Stretchy portions cannot extend partway - it's exactly double or no extension at all.
Scoring
This is code-golf. The shortest submission in bytes wins. Tiebreaker is earlier answer.
>
wouldn't become<
either, same for for(
and)
), but he also says "It's only important that slashes alternate within the sequence of vertical bars they replaced. Their ordering in the snake at large or whether a forward or backward slash comes first doesn't matter." \$\endgroup\$