Build a triangle without any triangles
Ruby, 79
->n{1.upto(n*=3){|i|puts (' '*(n-i)).ljust(n+i,'/__\_\/\ \__/_/\/ '[i%3*6,6])}}
A. (-4 bytes, -1
+1
) changed from 0-indexed (.times
) to 1-indexed (1.upto
)
B. (-5 bytes) changed from array of three 6-char strings to selection of 6-char substring of 18-char string.
C. (-1 byte) m=n*3
-> n*=3
D. (-5 bytes) reduced all five double backslashes to single backslashes (partly made possible by reordering of string required for point A)
Ruby, 94
->n{(m=n*3).times{|i|puts (' '*(m-i-1)).ljust(m+i+1,[ '/\\ \\__','/_/\\/ ','/__\\_\\'][i%3])}}
explanation
The basic unit is a 3x6 diamond as follows (last character on each row duplicated for clarity:)
/\ \__/
/_/\/ /
/__\_\/
All we need to do is display a suitable window of this pattern. Ruby's ljust
allows you to pad with any string, not just spaces. Normally ljust
would be used to pad a string of printable characters by adding spaces at the end, but here we use it in reverse: to pad a string of spaces by adding printable characters at the end.
ungolfed in test program
f=->n{
(m=n*3).times{|i| #for each line of the triangle
puts (' '*(m-i-1)). #print m-i-1 spaces,
ljust(m+i+1,[ '/\\ \\__', #left justified and padded to total length m+i+1
'/_/\\/ ', #by one of these three strings
'/__\\_\\'][i%3])
}
}
f[gets.to_i]
CJam, 47
ri_"/__\_\/_/\/ /\ \__"6/f**eeW%{_S.*s\~,\-<N}/
Explanation:
ri_ read the input, convert to integer and duplicate
"…" push that string, containing the repeating pattern
(3 lines in reverse order, concatenated)
6/ split into (3) lines of 6 characters
f* multiply (repeat) each line n times
* repeat the array of 3 lines n times
at this point we have an array of 3*n strings with 6*n characters each
ee enumerate the array (obtaining an array of [index string] pairs)
W% reverse the array
(so the lines are in correct order and indices in reverse order)
{…}/ for each [index string] pair
_ duplicate the pair
S.* vectorized-multiply with " "
this effectively replaces the index with a string of <index> spaces
s convert the pair to string, effectively concatenating the spaces
with the string
\ swap with the other copy of the [index string] pair
~, dump the index and string on the stack and get the string length
\- subtract the index from it - this is the desired line length
< cut the concatenated string to that length
N add a newline
Try it online
Retina, 150 122 118 bytes
The output to this challenge looks awesome, by the way!
Input is in unary. The output contains a trailing linefeed. The code uses ISO 8859-1 encoding. Note the trailing space on the penultimate line.
(?=_\\¶.*1)
_\/__\
(?=/_/\\¶.*1)
/_/\/
(^|__)(?=/\\¶.*1)
$1/\ \__
ms}`(.*1*)1
/\¶/_/\¶/__\_\¶$1
m`^(?=(.*¶)*.)
$#1$*
Try it online
Explanation
If you want a more in-depth explanation, comment or message me in chat.
(?=_\\¶.*1) # Matches the start of the 3rd line of every triangle
/__\_\ # and prepends another layer if needed
(?=/_/\\¶.*1) # 2nd line of each triangle
/_/\/
(^|__)(?=/\\¶.*1) # 1st line of each triangle
$1/\ \__
ms}`(.*1*)1 # This and above in a multi-single-line loop.
/\¶/_/\¶/__\_\¶$1 # This stage adds a flattened triangle on top
m`^(?=(.*¶)*.) # Prepend a space for every line following -1
$#1$*
Thanks to Martin for golfing 32 bytes off.