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:
+------------+
| |
+--+-+ +----+-+
| | | | | |
| | | +---+--+ | |
| | | | | | | |
+--+-+ | +-+--+-+-+-+
| | | | | | | |
| | | | | | | |
| | | | | | | | +-+
| +-+-+--+ | | | | |
| | | | | | +-+-+-+
+-----+-+----+ | | | | | |
| | | | | +-+ |
| +------+ | | |
| | | |
+----------+ +-----+
Additional Notes:
- 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.
+
for it's top-left corner, and then (immediately below) the+
for it's bottom-left corner? \$\endgroup\$