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.
asked Jun 7, 2017 at 13:21
\$\endgroup\$
6
  • \$\begingroup\$ The first box in the second column does not explode at the bottom part. I think this is not the desired output. \$\endgroup\$ Commented Jun 7, 2017 at 14:19
  • \$\begingroup\$ @GáborFekete Yes, my idiocy again. :I Thanks for pointing that out! :) \$\endgroup\$ Commented Jun 7, 2017 at 14:19
  • \$\begingroup\$ Must the box be left-aligned? \$\endgroup\$ Commented Jun 7, 2017 at 14:55
  • \$\begingroup\$ "padding spaces are not needed." Are they allowed? \$\endgroup\$ Commented Jun 7, 2017 at 15:17
  • \$\begingroup\$ @Neil I will say yes. \$\endgroup\$ Commented Jun 7, 2017 at 22:12

3 Answers 3

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.

answered Jun 7, 2017 at 16:43
\$\endgroup\$
2
\$\begingroup\$

Charcoal, (削除) 56 (削除ここまで) 49 bytes

SχA0δW¬=×ばつ|÷+1δ2‖OO%Lχ2‖OO↓%δ2

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.

answered Jun 8, 2017 at 0:22
\$\endgroup\$
1
  • \$\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⎇κφε↷¹¶↷4+↶¹P⎇κεφ↷¹×¶⎇κδχ. \$\endgroup\$ Commented Jun 30, 2017 at 23:59
1
\$\begingroup\$

Jelly, (削除) 80 79 (削除ここまで) 77 bytes

HĊḢ
ċЀ)-|HμHĊS+=0Ḣ$©Ṭ€ḤH8ÇR¤¦;8ドルÇo1¤¦3Ḋ1ドル®?z0ZUŒḄm08ドルḂḢ¤?€ŒḄm08ドルḂṪ¤?ị"-|+ "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Ḣ$©Ṭ€ḤH8ÇR¤¦;8ドルÇo1¤¦3Ḋ1ドル®?z0ZUŒḄm08ドルḂḢ¤?€ŒḄm08ドルḂṪ¤?ị"-|+ "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]]
ḤH8Ç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:
 8 - 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]]
;8ドルÇ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:
 8 - 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]]
Ḋ1ドル®? - 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:
 1 - 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]]
ŒḄm08ドルḂḢ¤?€ - perform reflections of row elements
 ?€ - if for €ach:
 ¤ - nilad followed by link(s) as a nilad:
 8 - 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
ŒḄm08ドルḂṪ¤? - perform reflection of rows
 ? - if:
 ¤ - nilad followed by link(s) as a nilad:
 8 - 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:
 >>> - 
 >>> +- -+ 
 >>> | | 
 >>>| |
 >>>| |
 >>> | | 
 >>> +- -+ 
 >>> - 
answered Jun 7, 2017 at 22:52
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.