Draw some mountain peaks

Write a program or function that draws a mountain range, where each subsequent larger mountain peak is "behind" the ones in front, and alternates which side is visible.

This is a mountain range of size 1

/\


This is a mountain range of size 2

 /\
/\ \


This is a mountain range of size 3

  /\
/ /\
/ /\ \


This is a mountain range of size 4

   /\
/\ \
/ /\ \
/ /\ \ \


This is a mountain range of size 5

    /\
/ /\
/ /\ \
/ / /\ \
/ / /\ \ \


And so on.

Input

A single positive integer in any convenient format, n > 0.

Output

An ASCII-art representation of the mountain range, following the above rules. Leading/trailing newlines or other whitespace are optional, provided that the peaks line up appropriately.

Rules

• Either a full program or a function are acceptable. If a function, you can return the output rather than printing it.
• Standard loopholes are forbidden.
• This is so all usual golfing rules apply, and the shortest code (in bytes) wins.

Charcoal, 16 bytes

ＮλＦλ«Ｐ↘⁻λι←↙¹‖Ｔ→


Try it online!

How?

Ｎλ inputs the size of the largest mountain into λ. Ｆλ« runs a loop over values of ι from 0 through λ-1. (The closing » is implied at the end of the program.)

Inside the loop, Ｐ↘⁻λι calculates λ-ι and draws, without moving the cursor afterward, a line of that length going southeast. Based on its direction, this line will consist of \ characters. ← moves one step to the west, and ↙¹ draws a line of length 1 going southwest (made of /). Finally, ‖Ｔ→ horizontally reflects the drawing, transforming characters as appropriate: \ becomes / and / becomes \.

Adding the dump instruction Ｄ at the beginning of the loop (try it) allows us to see the progression:

    /\
/
/
/
/
/\
/\ \
/    \
/      \
/        \
/\
/ /\
/ /\ \
/ /    \
/ /      \
/\
/\ \
/ /\ \
/ /\ \ \
/ /    \ \
/\
/ /\
/ /\ \
/ / /\ \
/ / /\ \ \

• I guess this is the ideal language for the challenge :-) – ETHproductions Nov 4 '16 at 19:30
• @ETHproductions This language is created specifically for ASCII art. It also has its own codepage. – Erik the Outgolfer Nov 4 '16 at 20:53
• @EriktheGolfer I have my own (unimplemented) ASCII art language, called Crayon. The docs are kind of lost at the moment because I'm in the middle of moving them, but you can see most of them here. I wonder how short a Crayon program for this would be in comparison to Charcoal... – ETHproductions Nov 4 '16 at 21:08
• @ETHproductions When are you going to implement it? Is there a chatroom for it or can I make one (called "Crayon Implementation")? – Erik the Outgolfer Nov 4 '16 at 21:10
• @EriktheGolfer Unfortunately, I don't know when I'll have time to implement it. But you can create a chatroom if you'd like ;-) – ETHproductions Nov 4 '16 at 21:13

JavaScript (ES6), 75 bytes

for(n=prompt(s="/\\");n--;s=n%2?s+' \\':'/ '+s)console.log(" ".repeat(n)+s)


The full program is currently slightly shorter than the recursive function:

f=n=>n?" ".repeat(--n)+/\\
+f(n).replace(/\S.+/g,x=>n%2?x+" \\":"/ "+x):""


Python 2, 67 bytes

n=input()
s='/\\'
while n:n-=1;print' '*n+s;s=['/ '+s,s+' \\'][n%2]


Prints line by line, accumulating the string s by alternately adding a slash to the left or right based on the current parity of n. Prefixes with n spaces.

An alternative way to update was the same length:

s=n%2*'/ '+s+~n%2*' \\'
s=['/ '+s,s+' \\'][n%2]


A recursive method was longer (70 bytes).

f=lambda n,s='/\\':n*'_'and' '*~-n+s+'\n'+f(n-1,[s+' \\','/ '+s][n%2])


0%_=""
n%s=(' '<$[2..n])++s++'\n':(n-1)%(cycle[s++" \\","/ "++s]!!n) (%"/\\")  Usage: putStrLn$ f 5
/\
/ /\
/ /\ \
/ / /\ \
/ / /\ \ \


Prints line by line, accumulating the string s by alternately adding a slash to the left or right based on the current parity of n. Prefixes with n-1 spaces.

Batch, 202 bytes

@echo off
set/af=%1^&1
set m=/\
set s=
for /l %%i in (2,1,%1)do call set s= %%s%%
for /l %%i in (2,1,%1)do call:l
:l
echo %s%%m%
set s=%s:~1%
set/af^^=1
if %f%==1 (set m=%m% \)else set m=/ %m%


Takes input as a command-line parameter. Falls through to execute the last loop.

• @TimmyD Sorry, I typoed while golfing. Fixed now. – Neil Nov 4 '16 at 20:47

Ruby, 61 bytes

A pretty straightforward port of ETHproductions' JavaScript answer.

->n{s="/\\"
(puts" "*n+s
s=n%2>0?s+" \\":"/ "+s)until 0>n-=1}


See it on repl.it: https://repl.it/EPU5/1

Ungolfed

->n{
s = "/\\"
( puts " "*n+s
s = n%2 > 0 ? s+" \\" : "/ "+s
) until 0 > n -= 1
}


Haskell, 117 107 105 97 90 bytes

b!1=["/\\"]
b!n|m<-(1-b)!(n-1)=map(' ':)m++[[("/ "++),(++" \\")]!!b\$last m]
(unlines.(1!))


Try it on Ideone. Edit: Saved 8 bytes with an idea from Neil.

Ungolfed version:

p b 1 = ["/\\"]
p b n = let m = p (1-b) (n-1)
k = last m
r = map (' ':) m
in if b == 1
then r ++ [k ++ " \\"]
else r ++ ["/ " ++ k]
f n = unlines(p 1 n)


Recursive approach. The shape for n is generated by adding a space in front of each line of the n-1 shape and taking the last line of n-1 and add "/ " before if n is odd or " \" after if n is even ... or so I thought before noticing that this last step is reversed for all recursive steps when the final n is odd. Therefore a flag b is passed which alternates each recursive call and determines if the next mountain part is added left or right.

• Rather than comparing b to odd n each time, can you not just pass a flag in at the beginning and flip it on every recursive call? Something like f n = unlines(p 0 n) and let m = p (1-b) (n-1). – Neil Nov 4 '16 at 20:46
• Flipping to -b is another char off. – xnor Nov 5 '16 at 0:05
• @xnor Thanks for the hint, but I found another way to golf it further which needs b to be 0 or 1. – Laikoni Nov 5 '16 at 11:07

Java 7,130 bytes

String f(int n,String s){String l="";for(int i=1;i++<n;l+=" ");return n>1?n%2<1?l+s+"\n"+f(--n,s+" \\"):l+s+"\n"+f(--n,"/ "+s):s;}


Ungolfed

class Mountain {
public static void main(String[] args) {
System.out.println(f( 5 , "/\\" ) );
}
static String f(int n,String s){
String l = "";
for (int i = 1; i++ < n; l += " ") ;
return n > 1? n % 2 < 1?l + s + "\n" + f(--n , s + " \\")
:l + s + "\n" + f(--n , "/ " + s)
:s;
}

}

• Nice answer, +1. You can golf it by 2 bytes though: n%2 to n--%2, and both --n to n. EDIT: And 1 more by adding ,x=s+"\n" and changing both s+"\n" to x. (So in total: String f(int n,String s){String l="",x=s+"\n";for(int i=1;i++<n;l+=" ");return n>1?n--%2<1?l+x+f(n,s+" \\"):l+x+f(n,"/ "+s):s;} 127 bytes) – Kevin Cruijssen Nov 7 '16 at 13:55

C++ 138(function)

Function :-

#define c(X) cout<<X;
#define L(X,Y) for(X=0;X<Y;X++)

void M(int h){int l=1,r=1,j,H=h,i;L(i,h){for(j=H;j>0;j--)c(" ")L(j,l)c(" /")L(j, r)c("\\ ")c("\ n")(h%2)?(i%2)?r++:l++:(i%2)?l++:r++;H--;}


Full program :-

#include<conio.h>
#include<iostream>

using namespace std;

#define c(X) cout<<X;
#define L(X,Y) for(X=0;X<Y;X++)

void M(int h)
{
int l=1,r=1,j,H=h,i;

L(i, h)
{
for (j = H;j > 0;j--)
c(" ")
L(j, l)
c(" /")
L(j, r)
c("\\ ")
c("\n")

(h % 2) ? (i % 2) ? r++ : l++ :(i % 2) ? l++ : r++;
H--;
}
}

int main()
{
int h;
cin >> h;
M(h);
_getch();
return 0;
}


NOTE : the function _getch() may have different prototype names across different compilers.