7
\$\begingroup\$

This is very similar to this challenge, "Implode the Box". It was a pretty nice challenge and it didn't get many answers, so I'm going to post a similar challenge (strongly) inspired by it.

ASCII Boxes look like this:

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

Here are the same ASCII boxes, exploded:

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

Here are the test cases in a more computer-friendly format.

The challenge is to take an ASCII box as input and output the exploded box. A box is exploded according to the following rules:

  1. + never changes, and neither do the - and | immediately neighboring them.
  2. Starting from the corners and moving in, each - and | move outward one more space than the previous one.

The result does not have to be symmetrical, so padding spaces are not needed.

  1. Standard loopholes apply.
  2. You can take input in any reasonable format, including LF/CR/CRLF separated lines, array of lines, array of array of characters, etc.
  3. The only characters in the input string will be "+", "-", "|", " ", and "\n" (or whatever your newline is, if you choose this input format), and your output string should follow the same rules.
  4. You may optionally have a single trailing newline at the end of the last line.
  5. The smallest ASCII box you need to handle is the top-left example. Every ASCII box will have exactly 4 "+"s, exactly at its corners.
  6. You can assume you will always get a single valid ASCII box as input.
  7. The box must be left-aligned.
  8. Each line may have trailing whitespace so long as the line (without the newline character) is not longer than the longest line with all trailing whitespace removed. That is, the padding whitespace to the right cannot stretch out farther than the center of the extrusion.
\$\endgroup\$
  • \$\begingroup\$ The first box in the second column does not explode at the bottom part. I think this is not the desired output. \$\endgroup\$ – Gábor Fekete Jun 7 '17 at 14:19
  • \$\begingroup\$ @GáborFekete Yes, my idiocy again. :I Thanks for pointing that out! :) \$\endgroup\$ – HyperNeutrino Jun 7 '17 at 14:19
  • \$\begingroup\$ Must the box be left-aligned? \$\endgroup\$ – Neil Jun 7 '17 at 14:55
  • \$\begingroup\$ "padding spaces are not needed." Are they allowed? \$\endgroup\$ – Doorknob Jun 7 '17 at 15:17
  • \$\begingroup\$ @Neil I will say yes. \$\endgroup\$ – HyperNeutrino Jun 7 '17 at 22:12
4
\$\begingroup\$

Retina, 136 bytes

m`^
#
(¶#\|(.*))(?<=\1\1)(?=\1)
¶|  $2
T`#`_`^(.|¶#)*$
}T`#`p
+`^(.*?-)(-+)-
$.1$* $2¶$1$.2$* -
+`¶(.*?-)(-+)(-.*)$
¶$1$.2$* $3¶$.1$* $2

Try it online! Explanation:

m`^
#

Prefix a # to every line.

(¶#\|(.*))(?<=\1\1)(?=\1)
¶|  $2

Where there are three identical lines, i.e. in the middle of a box, expand the middle line of the three. (When there are more than tree identical lines, all except the first and last get expanded.)

T`#`_`^(.|¶#)*$

If no lines were expanded, then delete the #s again. This causes the loop to exit.

}T`#`p

If at least one line was expanded, change the #s to spaces, and continue the loop. (I use p here because it's easier to see.) At the end of the loop, the sides of the box will have been completely exploded.

+`^(.*?-)(-+)-
$.1$* $2¶$1$.2$* -

Repeatedly remove all but two of the top row of -s and put them on their own line preceding the rest of the original row. This explodes the top of the box.

+`¶(.*?-)(-+)(-.*)$
¶$1$.2$* $3¶$.1$* $2

Repeatedly remove all but two of the bottom row of -s and put them on their own line following the rest of the original row. This explodes the bottom of the box.

\$\endgroup\$
2
\$\begingroup\$

Charcoal, 56 49 bytes

SχA⁰δW¬⁼χSA⁺¹δδ↓+P↗×-÷⁺¹Lχ²↙×|÷⁺¹δ²‖OO﹪Lχ²‖OO↓﹪δ²

Try it online! Link is to verbose version of code. Explanation: The input box is measured; χ contains the first line of the box, while δ contains the number of rows of the box. The top left corner of the exploded box is then drawn: first the +, then the -s, then the |s. Finally the box is reflected horizontally and vertically; for boxes of odd sizes, a 1-character overlap is taken into account.

\$\endgroup\$
  • \$\begingroup\$ I'd originally tried drawing the whole box in a loop but even after golfing that still weighed in at 68 bytes: SχA¹δW¬⁼χSA⁺δ¹δA⁻Lχ¹χA×-÷χ²εA×|÷δ²φF²F²«↷¹P⎇κφε↷¹¶↷⁴+↶¹P⎇κεφ↷¹×¶⎇κδχ. \$\endgroup\$ – Neil Jun 30 '17 at 23:59
1
\$\begingroup\$

Jelly,  80 79  77 bytes

HĊḢ
ċЀ⁾-|HµHĊS+=0Ḣ$©Ṭ€ḤH⁸ÇR¤¦;€⁸Ço1¤¦3Ḋ€¹®?z0ZUŒḄm0$⁸ḂḢ¤?€ŒḄm0$⁸ḂṪ¤?ị“-|+ ”Y

A full program that prints the result.

Try it online!

How?

HĊḢ - Link 1: first item halved and rounded up: list
H   - halve (vectorises)
 Ċ  - ceiling (vectorises) (i.e. round up)
  Ḣ - head

ċЀ⁾-|HµHĊS+=0Ḣ$©Ṭ€ḤH⁸ÇR¤¦;€⁸Ço1¤¦3Ḋ€¹®?z0ZUŒḄm0$⁸ḂḢ¤?€ŒḄm0$⁸ḂṪ¤?ị“-|+ ”Y - Main link

breaking the Main link down:

ċЀ⁾-|Hµ - get the dimensions: list of characters
   ⁾-|   - literal ['-', '|']
 Ѐ      - map with:
ċ        - count - yields a list [number of '-'s, number of '|'s]
      H  - halve
       µ - start a new monadic link using that as it's argument, call it d

HĊS+=0Ḣ$©Ṭ€ - build an appropriately sized binary "diagonal":                  e.g. [3,4]
H           - halve (vectorises)                                                  [1.5,2]
 Ċ          - ceiling (vectorises) (rounds each halved dimension up)                [2,2]
  S         - sum                                                                       4
       $    - last two links as a monad:
    =0      -   equals zero? (vectorises)                                           [0,0]
      Ḣ     -   head (first element)  (i.e. "inputHasZeroDashes")                       0
        ©   - copy to register and yield
   +        - addition                                                                  4
         Ṭ€ - untruth for €ach                              [[1],[0,1],[0,0,1],[0,0,0,1]]

ḤH⁸ÇR¤¦ - make 1s become 2s for those that will represent '|' rather than '-'
Ḥ       - double (vectorises)                               [[2],[0,2],[0,0,2],[0,0,0,2]]
      ¦ - sparse application...
        - ... at indexes:
     ¤  -   nilad followed by link(s) as a nilad:
  ⁸     -     chain's left argument, d                                              [3,4]
   Ç    -     call last link (1) as a monad                                             2
    R   -     range                                                                 [1,2]
        - ...of:
 H      -   halve                                           [[1],[0,1],[0,0,2],[0,0,0,2]]

;€⁸Ço1¤¦3 - place a 3 where the `+` should be
        3 - literal 3                                                                   3
       ¦  - sparse application...
          - ... at indexes:
      ¤   -   nilad followed by link(s) as a nilad:
  ⁸       -     chain's left argument, d                                            [3,4]
   Ç      -     call last link (1) as a monad                                           2
     1    -     literal 1                                                               1
    o     -     logical or (when no '-' Ç returns 0, but we want 1)                     2
          - ...of:
;€        -   concatenate for €ach (append the 3)         [[1],[0,1,3],[0,0,2],[0,0,0,2]]

Ḋ€¹®? - a further correction for zero width edge-case
    ? - if:
   ®  -   recal from register (inputHasZeroDashes)
      - ...then:
Ḋ€    -   dequeue €ach (all rows were made one longer, so adjust)
      - ...else:
  ¹   -   identity (do nothing)                           [[1],[0,1,3],[0,0,2],[0,0,0,2]]

z0ZU - pad with zeros and reflect each
z0   - transpose with filler zero               [[1,0,0,0],[0,1,0,0],[0,3,2,0],[0,0,0,2]]
  Z  - transpose                                [[1,0,0,0],[0,1,3,0],[0,0,2,0],[0,0,0,2]]
   U - upend (reverse each)                     [[0,0,0,1],[0,3,1,0],[0,2,0,0],[2,0,0,0]]

ŒḄm0$⁸ḂḢ¤?€ - perform reflections of row elements
         ?€ - if for €ach:
        ¤   -   nilad followed by link(s) as a nilad:
     ⁸      -     chain's left argument, d                                          [3,4]
      Ḃ     -     modulo 2 (vectorises)                                             [1,0]
       Ḣ    -     head (first element)                                                  1
            - ...then:
ŒḄ          -   bounce  [[0,0,0,1,0,0,0],[0,3,1,0,1,3,0],[0,2,0,0,0,2,0],[2,0,0,0,0,0,2]]
            - else:
    $       -   last two links as a monad
  m0        -     reflect

ŒḄm0$⁸ḂṪ¤? - perform reflection of rows
         ? - if:
        ¤   -   nilad followed by link(s) as a nilad:
     ⁸      -     chain's left argument, d                                          [3,4]
      Ḃ     -     modulo 2 (vectorises)                                             [1,0]
       Ṫ    -     tail (last element)                                                   0
            - ...then:
ŒḄ          -   bounce
            - else:
    $       -   last two links as a monad
  m0        -     reflect
            -           [[0,0,0,1,0,0,0],[0,3,1,0,1,3,0],[0,2,0,0,0,2,0],[2,0,0,0,0,0,2],
            -            [2,0,0,0,0,0,2],[0,2,0,0,0,2,0],[0,3,1,0,1,3,0],[0,0,0,1,0,0,0]]

ị“-|+ ”Y - create the list of characters itself
 “-|+ ”  - literal ['-', '|', '+', ' ']
ị        - index into                           ["   -   "," +- -+ "," |   | ","|     |",
         -                                       "|     |"," |   | "," +- -+ ","   -   "]
       Y - join with newlines
         -       "   -   \n +- -+ \n |   | \n|     |\n|     |\n |   | \n +- -+ \n   -   "
         - implicit print:
           >>>   -   
           >>> +- -+ 
           >>> |   | 
           >>>|     |
           >>>|     |
           >>> |   | 
           >>> +- -+ 
           >>>   -   
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.