Suppose one day you are digging through your big box of unused computer cords and adapters (USB to USB mini, VGA to DVI, etc.). There are tangled cords everywhere making quite a mess, and you wonder if you could simplify things by attaching all the cords together in one long strand, and then just rolling it up.
The question is, is it possible to connect all of your cords and adapters in one long line like this? It's obviously not always possible, e.g. if you only had two cords with completely different plugs, they could not be connected together. But if you had a third cord that can connect to both of them, then you could string all your cords together.
You don't care about what type of plugs are on the ends of the all-cord strand. They don't need to plug into one another to form a loop. You only want to know if making the all-cord strand is possible, and if it is, how to do it.
Challenge
Write a program or function that takes in a multiline string where every line depicts one of the cords you own. A cord is made up of one or more dashes (-
), with a plug on either end. A plug is always one of the 8 characters ()[]{}<>
.
So these are some valid cords:
>->
(--[
}-{
<-----]
(---)
But these are not:
-->
(--
)--
[{
---
When connecting cords, only plugs with the exact same bracket type can be connected together.
So these are some valid cord connections:
...---((---...
...---))---...
...---]]---...
...---{{---...
...---<<---...
And these are invalid:
...---()---...
...---)(---...
...---{]---...
...---{[---...
...---><---...
...--->)---...
If all the cords in the input can be rearranged and attached together in one long strand, then output that strand to stdout on one line (with an optional trailing newline). When there are multiple solutions you can choose any one of them to output. If making a single strand is not possible, then output nothing (or output an empty string with an optional trailing newline).
For example, if the input is
[-->
{---]
>----{
the output could be
[-->>----{{---]
where all the cords are strung together.
However if the input were
[-->
{---]
the cords cannot be connected so there would be no output.
Note that cords can be flipped around as much necessary to make connections. e.g. [-->
and <--]
are effectively the same cord because they can make the same type of connections. Some outputs may depend on flipping the input cords.
For example
(-[
}--]
could have output
(-[[--{
where the second cord is flipped, or
}--]]-)
where the first cord is flipped.
(Note that in general flipping the entire output is valid because it's the same as initially flipping every cord individually.)
The lengths of the cords in the output should of course match the lengths of the corresponding input cords. But the cords may be reordered and flipped around as much as you want in order to make the all-cord strand. The input will always contain at least one cord.
The shortest code in bytes wins.
Test Cases
Cases with output:
[-->
{---]
>----{
gives
[-->>----{{---]
or
[---}}----<<--]
(-[
}--]
gives
(-[[--{
or
}--]]-)
(-)
gives
(-)
[--{
gives
[--{
or
}--]
[-]
]-[
gives
[-]]-[
or
]-[[-]
[----->
)------------[
{--<
}---)
could give
[----->>--}}---))------------[
or
>--}}---))------------[[----->
or
}---))------------[[----->>--}
or
{--<<-----]]------------((---{
etc.
>-->
>->
>--->
could give
>-->>->>--->
or
>--->>-->>->
or
>->>-->>--->
or
<--<<---<<-<
etc.
(-]
]->
>-}
}-)
)-[
[-<
<-{
{-(
could give
(-]]->>-}}-))-[[-<<-{{-(
or
{-((-]]->>-}}-))-[[-<<-{
or
<-{{-((-]]->>-}}-))-[[->
etc.
Cases without output:
[-->
{---]
[-]
[-]
(-]
]->
}-)
>->
>-->
]---]
[-------------------]
]-------------------[
[-----------------]
[-----------------]
{--[
]--}