# Connect the Dots

Given a series of os representing dots, connect them vertically or horizontally

## Examples

Input:

o   o

o


Output:

o---o
|
|
o


Input:

o   o    o

o


Output:

o---o----o
|
o


## Spec

• There will only be o, spaces, and newlines in the input

• There will always be a pair of dots to connect
• No two os will be directly adjacent
• Dots should be connected with | and -, for vertical and horizontal connections respectively
• No dot connections will overlap
• Do you have to connect every legal pair or just connect the dots into 1 component? Can the dots always be connected into 1 component? "There will always be at least two dots to connect" would make more sense if I understand it right. A couple test cases could clear these up too. – randomra Feb 13 '16 at 0:01
• @randomra you have t connect every legal pair, they won't always be 1 component – Downgoat Feb 13 '16 at 0:02
• @Downgoat How about some more test cases that cover disconnected components and loops within a single component then? ;) – Martin Ender Feb 13 '16 at 13:07

# Japt, 33 29 bytes

Uy eV="o +o"_rS'|} y eV,_rS'-


Test it online!

### How it works

Uy         // Transpose rows with columns in the input.
eV="o +o"  // Set V to the regex-string "o +o", and recursively replace each match Z with:
_rS'|}     //  Z with spaces replaced with "|"s.
y          // Transpose again.
eV,        // Recursively replace matches Z of V with:
_rS'-      //  Z with spaces replaced with "-"s.


## Ruby, 137 133 bytes

->s{eval"s.split($/).map(&:chars)#{".map{|x|x.join.gsub(/o +(?=o)/){|x|x.tr' ',?|}.chars}.transpose"*2}.map(&:join)*$/".sub'?|','?-'}


This is absolutely horrible. Still trying to golf.

eval"s.split($/).map(&:chars)#{".map{|x|x.join.gsub(/o +(?=o)/){|x|x.tr' ',?|} .chars}.transpose"*2}.map(&:join)*$/".sub'?|','?-'

• Alright, I'll input as a padded rectangle, but only because you asked so nicely. – Alex A. Feb 13 '16 at 1:07
• Can you use \b instead of (?=o)? – Justin Feb 13 '16 at 2:54
• @Justin Doesn't appear to work. :/ – Doorknob Feb 13 '16 at 4:06

## Retina, 80 bytes

T -o.+o
Tm |(?<=(?(1)!)^(?<-1>.)*o\D*¶(.)*) (?=(.)*¶\D*o(?<-2>.)*$(?(2)!))  Input needs to be padded. Try it online! ### Explanation The first stage is pretty simple, it just turns all spaces into hyphens which are found between two os in the same line. The second stage covers the |s. This is a bit trickier and requires balancing groups. The lookbehind (?<=(?(1)!)^(?<-1>.)*o\D*¶(.)*)  checks that there's an o earlier in the same column. Remember that lookbehinds should be read from right to left. (.)* stores the horizontal position of match, \D*¶ checks skips to any character in the preceding lines, o matches literally. Then (?(1)!)^(?<-1>.)* ensures that the horizontal position of that o is the same. The lookahead (?=(.)*¶\D*o(?<-2>.)*$(?(2)!))


Does exactly the same thing in the opposite direction.