The Descent - Puzzle discussion

You are welcome! :slight_smile:

It’s interesting how does the stream move over one mountain in case 2 Scattered mountains. Can someone explain, in a for loop when the max variable equals 9 after first assignment (after first mountain with its high 9, we have output 0), this variable still will be 9 (for the second mountain is 8 which is less then 9, judging from this line of code if (mountainH > max), our output is 1 now. Afer third mountain the variable max still will be 9, for the third mountain equals 7 and it’s also less then 9, so we have output 2 now and so on untill we come to the fourth mountain which equals 3 but then the output of a program chooses the fifth mountain with its hight 6. Why is that??? How does this code work then? Sorry if my question is stupid, just want to understand)

The max variable does not remain 9 for the whole time. Before each round of comparison, you reset the max variable to 0. For example,

In Round 1, max is initialised as 0 and after all the comparison, max becomes 9.
In Round 2, max is again initialised as 0 and after all the comparison, max becomes 8.
In Round 3, max is again initialised as 0 and after all the comparison, max becomes 7.
And so on and so forth.

Thanks. But when the turn of the fourth mountain comes, which hight is 3, which is greater then zero as well, the stream shoots the fifth mountain. Why?

Because you compare EVERY mountain in each round, and always store the HIGHEST one whenever one is found in that round.

That is for the while (true) loop? It would be nice to see how variables are changing at each turn but perhaps there is no such option here.

You can always add debug code to print out and see how variables change. The base code shows you how.

The input for one game turn consists of 8 lines: one integer mountainH per line - that was the answer! I thought it was one integer on the input and output.

1 Like

This is my code in C++ and it works

[nope]

Please don’t share solutions on the forum.

Can i have the solution with php please or a hint I don’t understand what to do

Click HINTS on the left of the puzzle statement.

I dunno if the instructions is not clear or because I’m a just a beginner.

Hi. So I am trying to solve this using JavaScript, and what I came up with looks like this:

while (true) {
for (let i = 0; i < 8; i++) {
const mountainH = parseInt(readline());
}

console.log(`${i}`);

And it doesn’t work, I get a timeout error. Why?

I think your console shows more than just a timeout error. When I tried your code, the console showed:

ReferenceError: i is not defined

at Answer.js. on line 16
Game information:

Timeout: your program did not write to the standard output in due time.

So you have to fix the issue as shown. Also remember that your task is to output the index of the highest mountain, and your code is not doing that. Read the hints (click the button to the left of the puzzle statement) for more help.

Well… I have managed to solve the first test case by removing everything I did not understand. Looks like this:
for (let i = 0; i < 8; i++) {
console.log(${i});
}
I am both ashamed and proud of this solution. Anyway, I guess I know which way to think next. Thanks for your help and your quick answer!

1 Like

new to js, i understand everything about the solution, except what is causing the ship to shoot the mountains?

Your output is the input to the game engine, which controls the animation (shooting).

Yes My code Same With you Bro, ```
let map = new Map();

while (true) {

for (let i = 0; i < 8; i++) {

    const mountainH = parseInt(readline()); // represents the height of one mountain.

    map.set(i, mountainH);

}

const mapSort1 = new Map([...map.entries()].sort((a, b) => b[1] - a[1]));

for (let index of mapSort1.keys()) {

    console.log(index); 
}

}

You may refer to the reply to that post for how to correct your code.