How many 14ers did I climb?
C, 174 bytes
a[99],c,p,j,M,m;main(i){for(i=j=1;c=getchar(),~c;i++)c<11?a[i]=j++,i=0:c&4?a[i]=j:0;for(m=j;c=a[i++];c>a[i-2]?M-m>1&&c-m>1?M=c,m=j,p++:M<c?M=m=c:M:m>c?m=c:0);printf("%d",p);}
Requires a trailing newline in the input, otherwise +4 bytes.
JavaScript (ES6), 133 bytes
s=>[...s].map((_,i)=>(c=s[i%h*w+i/h|0])=="/"?++a>2&&(p+=!d,d=a=3):c=="\\"&&--a<1&&(d=a=0),w=s.search`
`+1,h=-~s.length/w,a=3,d=p=1)|p
Explanation
Since the specifications are not stated clearly, this makes a couple of assumptions:
- The bottom line is the 14,000ft mark (so all positions on the grid are high enough to count as a peak).
- The grid starts at (or ascending) the first peak (since it's at least 14,000ft high already as per previous assumption).
- A separate peak counts only after descending 300ft then ascending 300ft.
Iterates over the character c
of each column (specifically, it iterates down each column until it finds a character). The current altitude is stored in a
. It is clamped to a minimum of 0
and a maximum of 3
. The direction needed to move to count the next peak is stored in d
(false
= up, true
= down). If a
reaches 3
and d
is false
, the number of peaks p
is incremented and d
is set to true
(down). Once a
reaches 0
, d
is set back to false
(up).
var solution =
s=>
[...s].map((_,i)=> // loop
(c=s[i%h*w+i/h|0]) // c = current character (moves down columns)
=="/"? // if c is '/'
++a>2&& // increment a, if a equals 3 and d is true:
(p+=!d,d=a=3) // increment p, set d to true, clamp a to 3
:c=="\\"&& // if c is '\':
--a<1&& // decrement a, if a equals 0:
(d=a=0), // set d to false, clamp a to 0
// Initialisation (happens BEFORE the above code)
w=s.search`\n`+1, // w = grid width
h=-~s.length/w, // h = grid height
a=3, // a = current altitude (min = 0, max = 3)
d= // d = required direction (false = up, true = down)
p=1 // p = number of found peaks
)|p // output the number of peaks
var testCases = [`
/\\
`,`
/\\
\\
\\ /\\
\\ / \\
\\/ \\
`,`
\\ /
\\ /
\\/
`,`
/\\
/\\/ \\/\\
/\\/ \\/\\
/\\/ \\/\\
/\\/ \\/\\
`,`
/\\__/\\
/ \\
/ \\
`,`
/\\ /\\
/ \\ / \\
/ \\/ \\
/ \\
`,`
/\\ /\\
/ \\/\\ / \\
/ \\/ \\
/ \\
`,`
/\\___/\\_
/ \\ /\\
/ \\ / \\
_/\\/ \\/ \\
/ \\
/ \\
/ \\_
`,`
/\\ /\\
/\\ / \\ /
/\\ / \\ / \\/
/ \\ / \\ /
/ \\/ \\/
`,`
/\\
_/\\__/ \\
/ \\
`,`
/\\
/ \\ /\\
/ \\_/ \\
/ \\
/ \\
/ \\
/ \\
`,`
/\\
/\\_/\\ / \\_
/ \\ / \\ /\\
/ \\/ \\ /
/ \\_/
/
`];
result.textContent = testCases.map(c=>c+"\n"+solution(c.slice(1,-1))).join`\n\n`;
<textarea id="input" rows="6" cols="40"></textarea><br /><button onclick="result.textContent=solution(input.value)">Go</button><pre id="result"></pre>