# Detonate a string

Given any string, print it in the form of a triangle where the text runs up and down along each diagonal. For example, an input of "Hello World" should output:

                    d
l
r   d
o   l
W   r   d
o   l
o   W   r   d
l       o   l
l   o   W   r   d
e   l       o   l
H   l   o   W   r   d
e   l       o   l
l   o   W   r   d
l       o   l
o   W   r   d
o   l
W   r   d
o   l
r   d
l
d


The space between each character in a row must be at least 1, in order to maintain the proper format.

• May we assume the string isn't empty? – Mr. Xcoder Aug 14 '17 at 13:09
• @Mr.Xcoder Yes you may – machiavelli Aug 14 '17 at 13:15
• The space between each character in a row must be at least 1: Does this mean that H l o W r d is a valid center row? Asking because in your example each row has 3 spaces between each character. – Emigna Aug 14 '17 at 13:33
• @Emigna I misunderstood you question, my apologies. Yes, your example would be valid. – machiavelli Aug 14 '17 at 13:37
• Leading or trailing spaces allowed? – Luis Mendo Aug 14 '17 at 13:45

# Charcoal, 10 7 bytes

↗ＥＬθ✂θιＵＥ¹

Try it online! Try it online! Links are to verbose version of code. Explanation:

    ↗       Print up and to the right
ＥＬθ✂θι All suffixes of the input, as a list down and to the right
ＵＥ¹     Insert blank columns

First time I got to use the ＵＥ command.

• I knew Charcoal would be one of the first answers here.. Was almost tempting to start myself, but I'm way to inexperienced with it to finish a solution in time, and it would get out-golfed anyway.. ;) – Kevin Cruijssen Aug 14 '17 at 13:11
• @Emigna ...but this was my big chance to use ＵＥ... – Neil Aug 14 '17 at 14:25
• @EriktheOutgolfer ...but this was my big chance to use ＵＥ.. – Neil Aug 14 '17 at 14:50
• @Neil It's -3 bytes! -3 for a nice sacrifice! Who doesn't want some nice -3? – Erik the Outgolfer Aug 14 '17 at 14:53
• @EriktheOutgolfer What you're supposed to say is, "You can't let 05AB1E beat you, can you?" – Neil Aug 14 '17 at 15:21

# 05AB1E, 108 7 bytes

Thanks to Emigna for saving 2 bytes!

ðâƶ.cðζ


Uses the 05AB1E encoding. Try it online!

• As per this line The space between each character in a row must be at least 1 you can remove ¶«. (also verified the validity with OP ) – Emigna Aug 14 '17 at 13:39
• @Emigna Thanks! :) – Adnan Aug 14 '17 at 13:46
• Might want to update the Tio link :) – Mr. Xcoder Aug 14 '17 at 13:47
• @Mr.Xcoder ninja'd – Adnan Aug 14 '17 at 13:47
• Clever use of â in ðâ instead of Sð«! – Erik the Outgolfer Aug 14 '17 at 14:45

# Python 2, 75 bytes

s=input()
k=l=len(s)
while k>1-l:k-=1;m=abs(k);print' '*m+' '.join(s[m::2])


Try it online!

Ruud saved 3 bytes.

• This has been allowed by the OP: 78 bytes. The space on each row must be at least 1. – Mr. Xcoder Aug 14 '17 at 13:49
• 75 bytes – Arfie Aug 16 '17 at 12:15

# C, 867873 70 chars

for(int i=1,j=1-n;i=putchar(j*j<i*i&i-j?s[i-1]?:13:32)^13?i+1:++j<n;);


Try it online!

### Explanation

Naive implementation: two cycles, fill from top to bottom, left to right (99 bytes):

for(int j=1;j<n*2;j++){for(int i=0;i<n;i++)printf("%c ",(i+j)%2&&i+1>=abs(j-n)?s[i]:' ');puts("");}


Here, puts() just prints \n to the output. Let’s combine variable declarations and combine j++ with something (94 bytes):

for(int i,j=0;++j<n*2;){for(i=0;i<n;i++)printf("%c ",(i+j)%2&&i>=abs(j-n)?s[i]:' ');puts("");}


Good. Variable j has a range 0...2n; let it be within -n...n, this makes the math simpler. Notice that boolean expression at the right of && always has the value 0 or 1. This means we can replace && with &. 91 byte:

for(int i,j=-n;++j<n;){for(i=0;i<n;i++)printf("%c ",~(i+j)%2&i>=abs(j)?s[i]:' ');puts("");}


And now we realized we printing an extra space. And yeah, we don’t need printf() to print just a single symbol. 86 bytes:

for(int i,j=-n;++j<n;){for(i=0;i<n;i++)putchar(~(i+j)%2&i>=abs(j)?s[i]:' ');puts("");}


Even better. Notice that condition i * i>=j * j is same as i>=abs(j), but shorter. Let’s move puts() into for loop increment expression. And guess what? Actually, we don’t need the braces around i+j. 78 bytes:

for(int i,j=-n;++j<n;puts(""))for(i=0;i<n;i++)putchar(i*i>=j*j&~i+j?s[i]:' ');


Did you know that putchar() returns the character it has printed? Let’s use XOR to test numbers for equivalence. Let’s replace space with its ASCII code, 32. Remember that end-of-line character code is 13. And finally: did you know that GCC/Clang do support https://en.wikipedia.org/wiki/Elvis_operator ? 73 bytes:

for(int i,j=-n;++j<n;)for(i=0;putchar(i*i>=j*j&~i+j?s[i]?:13:32)^13;i++);


Finally, guess what? We don’t need two for loops. We can replace ugly ~i+j with just i-j. 70 bytes:

for(int i=1,j=1-n;i=putchar(j*j<i*i&i-j?s[i-1]?:13:32)^13?i+1:++j<n;);


Future work: change loop direction? This might save some bytes, if done properly.

# SOGL V0.12, 1310 9 bytes

ēI*@∑}¹╚H


This uses a feature that I just added, but was documented a while ago.

Try it here!
In that link , is added because this expects the input on the stack and { added because otherwise , would get executed every time in the loop

implicitly start loop over POP
ē            increase the variable E, on first push which will be 0
I           increase by 1
*          multiply the current character that many times
@∑        join with spaces
}     end loop
¹    wrap everything on stack in an array
╚   center vertically
H  rotate counter-clockwise


f s|l<-length s-1=[zipWith min s$(' '<$[1..abs x])++cycle"~ "|x<-[-l..l]]


Try it online!

# Gaia, 16 bytes

$:ċ⟪×$§×⟫†€|$¦tụ  Try it online! ### Explanation $                 Split into list of chars
:ċ               Push [1 .. len(input)]
⟪×$§×⟫† Apply this block to corresponding elements of the two lists: × Repetition$             Split into chars
§×           Join with spaces
€|      Centre align the rows
$¦ Split each line into chars t Transpose ụ Join each row with spaces, then join the rows together with newlines  # Python 2, 86 83 bytes -3 thanks to officialaimm lambda s,j=' '.join:map(j,zip(*(j(c*-~i).center(len(s)*2)for i,c in enumerate(s))))  Try it online! # Jelly, 15 bytes ;€⁶ṁ"JUz⁶ZUŒBZG  Try it online! # Octave, 596358 57 bytes @(s,a=@strjust)a([kron(+a(hankel(s)),[1 0;0 0]) '']',99)'  Try it online! • 99: good idea :-) – Luis Mendo Aug 14 '17 at 16:01 ## Java, 292 bytes (sorry) public class D{ public static void main(String[]r){ String s=r[0];int L=s.length(),n=L*2-1,x=L-1,d=-1,i,j;boolean a=false,o=L%2==1; for(i=0;i<n;i++){ for(j=0;j<L;j++)System.out.print(j<x||a&&j%2==(o?0:1)||!a&&j%2==(o?1:0)?' ':s.charAt(j)); System.out.println(); x+=d;if(x<0){x=0;d=1;}a=!a;}}}  • You can remove the newlines, otherwise, this looks pretty golfed! – Zacharý Aug 14 '17 at 22:31 • You can golf more: 1. boolean a=1<0,o=L%2>0;. 2. If you don't need i, use this loop: for(i=0;i++<n;). 3. You can get rid of o: j%2<L%2 then j%2>L%2. 4. Using d as flip takes so many characters: just do j<(x<0?-x:x). 5. You have way more variables than needed. 6. You don't need a full program: a lambda or method is enough. -- If you want a golfed Java example, check my answer. – Olivier Grégoire Aug 14 '17 at 22:51 # Haskell, 81 bytes f s|l<-length s=[[last$' ':[s!!i|i>=n,mod(n+i)2<1]|i<-[0..l-1]]|n<-abs<$>[-l..l]]  Try it online! # Java (OpenJDK 8), 116 bytes s->{for(int l=s.length(),i=-l;++i<l;)System.out.printf("%"+l+"s%n",s.substring(i<0?-i:i).replaceAll("(.).","$1 "));}


Try it online!

## Explanation

s->{                                // Consumer<String> lambda
for(int l=s.length(),i=-l;++i<l;)  // For each length between l and 1 and back to l,
System.out.printf("%"+l+"s%n",    // Print with align to right
s.substring(i<0?-i:i)           // skip the first |i| characters
.replaceAll("(.).","$1 ") // replace every even-positioned character with a space. ); }  # C++, 135 bytes Okay, here's my shot with C++: auto f=[&](auto f,int y)->void{ for(int i{};i<n;i++) putchar(y<0?f(f,y+1?i+1:n-1-i),'\n':i<y||i+y&1?' ':s[i]); }; f(f,-1); f(f,-2);  Try It Online (ideone)! # Haskell, 140 137 bytes (m#n)s=(\(i,x)->' ':(last$"  ":[x:" "|rem i 2==m&&i>n]))=<<zip[0..]s
g s=((++)=<<reverse.tail)$id=<<[[(0#n)s,(1#n)s]|n<-[-1,1..length s]]  Try it online! Saved 3 bytes thanls to Challenger5 I don't think that's optimal... f produces one of the lines (m = 0 or 1 is the modulo of the line number, n is the number of the line) g intercalates "odd" and "even" lines, and add to the result a mirror of itself. • You can save bytes by defining f as an infix function (as in (m#n)s=...) rather than a prefix function. – Esolanging Fruit Aug 16 '17 at 7:01 # Pyth, 25 bytes j+_Km++*d;@Qdtj;%2>QdUQtK  Try it here. # Jelly, 18 bytes JrLm€2Ṭ;€0a⁸o⁶ṚŒḄG  Try it online! ## Mathematica 105 Bytes (c=Characters@#;l=Length@c;StringRiffle@Table[If[Abs[j-l]<i&&EvenQ[j+i],c[[i]]," "],{j,1,2l+1},{i,1,l}])&  Maybe I could shave off another byte or two, but the character count overhead of dealing with strings in Mathematica makes simple challenges like this uncompetitive. # J, 54 bytes [:|:|.@i.@#(>@],~' '#~[)"_1[:(,' '&,)/&.>>:@i.@#<@#"0]  Try it online! (note that the output on TIO has a newline and three spaces, but that isn't from the function call -- it's probably just what the J interpreter does automatically). I think the general idea for solving this is right, but there are small things that I'm probably doing sub-optimally that are adding to the bytecount. ### Previous variants 55 bytes <:@+:@#{.[:|:|.@i.@#(],~' '#~[)"_1>:@i.@#(,' '&,)/@#"0]  56 bytes <:@+:@#{.[:|:|.@i.@#(],~' '#~[)"_1#{.[:(,' '&,)//.[:]\.]  # Explanation This will be split into a few functions. Also, I wasn't as thorough with latter parts of the explanation, so let me know if you want a better explanation for a certain part and I can edit that in. dup =. >:@i.@# <@#"0 ] space =. (,' '&,)/&.> pad =. |.@i.@# (>@],~' '#~[)"_1 ] trans =. |:  • dup duplicates each character as many times as its index (plus one) in the string • space inserts spaces between each character • pad pads the characters with the right amount of spaces • trans transposes the resulting matrix Sample call:  trans pad space dup 'abc' c b a c b c  ### Dup >:@i.@# <@#"0 ] >:@i.@# Indices of each character plus one # Length of the string i. Range [0,length) >: Add one <@#"0 ] Duplicate each character as many times as it index (plus one) "0 For each # ] Copy the character >:@i.@# as many times as it index < Box the result  The results are boxed to prevent J from padding the ends with spaces (since they're of uneven length). Sample call:  dup 'abc' ┌─┬──┬───┐ │a│bb│ccc│ └─┴──┴───┘  ### Space (,' '&,)/&.> &.> For each boxed element (,' '&,)/ Insert spaces between each  Sample call:  space dup 'abc' ┌─┬───┬─────┐ │a│b b│c c c│ └─┴───┴─────┘  ### Pad |.@i.@# (>@],~' '#~[)"_1 ] (>@],~' '#~[) Pad the right arg with spaces given by the left arg |.@i.@# Indices in reverse order i. # Range [0,length) |. Reverse  Basically, pad the first element with length - 1 spaces, the second with length - 2, etc. It also removes the boxing. Sample call:  pad space dup 'abc' a b b c c c  ### Transpose This is just the built-in function |: which takes the transpose of a matrix. • I used a similar approach, but avoided boxing. 45 bytes: |:@(-@i.@-@#|."0 1((,@,.~' '#~#)@$"0~1+i.@#)). it could surely be golfed further. this part -@i.@-@# is some low hanging fruit, most likely – Jonah Aug 15 '17 at 2:34
• @Jonah I can't quickly decipher how your answer works, so I'll leave it to you to post it if you would like to, as I like to include an explanation of my answer. I guess J for me right now is a write-only language. – cole Aug 15 '17 at 3:01

# q/kdb+, 55 bytes

Solution:

-1(+){{1_a,((2*y)#" ",z),a:x#" "}'[(|)c;1+c:(!)(#)x]x};


Example:

q)-1(+){{1_a,((2*y)#" ",z),a:x#" "}'[(|)c;1+c:(!)(#)x]x}"Hello World";
d
l
r d
o l
W r d
o l
o W r d
l   o l
l o W r d
e l   o l
H l o W r d
e l   o l
l o W r d
l   o l
o W r d
o l
W r d
o l
r d
l
d


Explanation:

TODO. ungolfed version is 66 bytes:

-1 flip{{1_a,((2*y)#" ",z),a:x#" "}'[reverse c;1+c:til count x]x};


Bonus:

To get the same output as the example (74 bytes):

q)-1(+){1_'raze{(a,((2*y)#" ",z),a:x#" ";(2*y+x)#" ")}'[(|)c;1+c:(!)(#)x]x}"Hello World";
d
l
r   d
o   l
W   r   d
o   l
o   W   r   d
l       o   l
l   o   W   r   d
e   l       o   l
H   l   o   W   r   d
e   l       o   l
l   o   W   r   d
l       o   l
o   W   r   d
o   l
W   r   d
o   l
r   d
l
d