Reverse Engineer Bracket Rectangles

Every programmer knows that rectangles are really fun. To exacerbate this fun, these cute and fuzzy diagrams can be transformed into groups of interwoven brackets.

This challenge is the inverse of my previous.

Let's say you have a group of interlocking rectangles like so:

+------------+
|            |
+--+-+     +----+-+
|  | |     |    | |
|  | | +---+--+ | |
|  | | |   |  | | |
+--+-+ | +-+--+-+-+-+
|   | | |  | | | |
|   | | |  | | | |
|   | | |  | | | |    +-+
|   +-+-+--+ | | |    | |
|     | |    | | |  +-+-+-+
+-----+-+----+ | |  | | | |
| |      | |  | +-+ |
| +------+ |  |     |
|          |  |     |
+----------+  +-----+

• No two +s will ever be adjacent
• No two rectangles will ever share an edge or corner
• There will only ever be at most one vertical edge in each column

The first step is to look at the left-most edge of any rectangle. Assign it one of four bracket types ({[<. I choose [.

+------------+
|            |
[--+-]     +----+-+
[  | ]     |    | |
[  | ] +---+--+ | |
[  | ] |   |  | | |
[--+-] | +-+--+-+-+-+
|   | | |  | | | |
|   | | |  | | | |
|   | | |  | | | |    +-+
|   +-+-+--+ | | |    | |
|     | |    | | |  +-+-+-+
+-----+-+----+ | |  | | | |
| |      | |  | +-+ |
| +------+ |  |     |
|          |  |     |
+----------+  +-----+

Now look at the second left-most rectangle. Since it overlaps a [ rectangle, it must be of a different type. I choose (.

(------------)
(            )
[--(-]     +----)-+
[  ( ]     |    ) |
[  ( ] +---+--+ ) |
[  ( ] |   |  | ) |
[--(-] | +-+--+-)-+-+
(   | | |  | ) | |
(   | | |  | ) | |
(   | | |  | ) | |    +-+
(   +-+-+--+ ) | |    | |
(     | |    ) | |  +-+-+-+
(-----+-+----) | |  | | | |
| |      | |  | +-+ |
| +------+ |  |     |
|          |  |     |
+----------+  +-----+

The next left-most rectangle does not intersect with any previous rectangle, but does nest within the previous. I choose to assign it ( again. It is normally a good guess to assign a rectangle the same type as what it is nesting inside if possible, but sometimes backtracking is needed.

(------------)
(            )
[--(-]     +----)-+
[  ( ]     |    ) |
[  ( ] (---+--) ) |
[  ( ] (   |  ) ) |
[--(-] ( +-+--)-)-+-+
(   ( | |  ) ) | |
(   ( | |  ) ) | |
(   ( | |  ) ) | |    +-+
(   (-+-+--) ) | |    | |
(     | |    ) | |  +-+-+-+
(-----+-+----) | |  | | | |
| |      | |  | +-+ |
| +------+ |  |     |
|          |  |     |
+----------+  +-----+

This next rectangle can be assigned [ again.

(------------)
(            )
[--(-]     +----)-+
[  ( ]     |    ) |
[  ( ] (---+--) ) |
[  ( ] (   |  ) ) |
[--(-] ( [-+--)-)-+-]
(   ( [ |  ) ) | ]
(   ( [ |  ) ) | ]
(   ( [ |  ) ) | ]    +-+
(   (-[-+--) ) | ]    | |
(     [ |    ) | ]  +-+-+-+
(-----[-+----) | ]  | | | |
[ |      | ]  | +-+ |
[ +------+ ]  |     |
[          ]  |     |
[----------]  +-----+

This next rectangle is kinda fun. It intersects both a ( and a [ rectangle, so I could call it a { rectangle (or < but nobody likes those).

(------------)
(            )
[--(-]     {----)-}
[  ( ]     {    ) }
[  ( ] (---{--) ) }
[  ( ] (   {  ) ) }
[--(-] ( [-{--)-)-}-]
(   ( [ {  ) ) } ]
(   ( [ {  ) ) } ]
(   ( [ {  ) ) } ]    +-+
(   (-[-{--) ) } ]    | |
(     [ {    ) } ]  +-+-+-+
(-----[-{----) } ]  | | | |
[ {      } ]  | +-+ |
[ {------} ]  |     |
[          ]  |     |
[----------]  +-----+

The last two rectangles aren't that bad. They can be of any two different types.

(------------)
(            )
[--(-]     {----)-}
[  ( ]     {    ) }
[  ( ] (---{--) ) }
[  ( ] (   {  ) ) }
[--(-] ( [-{--)-)-}-]
(   ( [ {  ) ) } ]
(   ( [ {  ) ) } ]
(   ( [ {  ) ) } ]    {-}
(   (-[-{--) ) } ]    { }
(     [ {    ) } ]  <-{-}->
(-----[-{----) } ]  < { } >
[ {      } ]  < {-} >
[ {------} ]  <     >
[          ]  <     >
[----------]  <----->

Reading off the rectangles, I get [(]([{))}]<{}>. This would be one possible output for the above input. Here's a list of many possible options, not exhaustive:

[(]([{))}]<{}>
<(>(<{))}>{()}
{<}[{(]>)}[<>]
any of the 4! permutations of ([{<, you get the idea...

Input

ASCII-art rectangles, on the assumptions that they are unambiguous (see notes above) and can be properly converted into a string of brackets. You may assume either no trailing spaces or padded to a rectangle, with optional trailing newline. There will be no leading whitespace.

Output

Any one of the valid bracket strings that obeys the intersection restrictions of the rectangles. Other than an optional trailing newline, there should be no other characters than brackets. The main rule is, if two squares intersect, then they should be assigned different bracket types.

Goal

This is code-golf, (lack of) quantity over quality.

• FYI "exacerbate" means "make a bad thing worse". Jan 8, 2016 at 10:31
• @PaulR I believe that was the point
– cat
Jan 8, 2016 at 12:32
• Oh, OK, evidently whatever the point was it went straight over my head! Jan 8, 2016 at 12:56
• Can we assume that each rectangle has some height? i.e. it cannot have a + for it's top-left corner, and then (immediately below) the + for it's bottom-left corner? Mar 28, 2016 at 0:01

Python 3, 519 bytes

def c(i):
a,b,c,d={},0,[],{}
for e in map("".join,zip(*i.split("\n"))):
if"+"in e:
f=e.index("+"),e.rindex("+")
if f in a:j=a.pop(f);d[j]=f+(d[j],len(c));c.append(j)
else:a[f]=b;d[b]=len(c);c.append(b);b+=1
g,h={},0
while d:
i={list(d)[0]};
for j,(k,l,m,n)in d.items():
for(o,p,q,r)in(d[v]for v in i):
if not(m>r or n<q or k>p or l<o or(q<m<n<r and o<k>l<p)):break