The Descent - Puzzle discussion

thx for the reply :+1:

did you use particles? what package did you use?

As i realized from your code you output is the HEIGHT of the highest mountain instead of outputting the INDEX of of the highest mountain. You should not only save the value of max height but also index of this mountain.

Hello, I need some help understanding why my code which appears to be exactly the same as the provided solution isnā€™t working.

my solution:

// game loop
      while (true) {
          var imax = 0;
          var max = 0;
           for (let i = 0; i < 8; i++) {
             var mountainH = parseInt(readline()); // represents the height of one mountain.
                 if(mountainH > max){
                    max = mountainH;
                    imax = i;
                }





           }

             // Write an action using console.log()
             // To debug: console.error('Debug messages...');
                print(imax);

                
               console.log('4'); // The index of the mountain to fire on.
          }



//hint provided solution
// game loop
        while (true) {
            var max = 0;
            var imax = 0;
              for (var i = 0; i < 8; i++) {
                  var mountainH = parseInt(readline()); 
                   if (mountainH > max) {
                     max = mountainH;
                    imax = i;
               }
          }

                   print(imax);
         }  

Iā€™ve been working on this for hours and then I find out it appears to be something with the challenge itself since Iā€™m not seeing anything different from what I am trying to run and the provided solution

Hi, so I noticed that the provided solution didnā€™t have this line:

console.log(ā€˜4ā€™); // The index of the mountain to fire on.

I removed it from my code and it passed.
which is extremely frustrating because if you reset your code the console.log(4) is automatically added which means that the module itself isnā€™t working correctly.

Heya, Iā€™m new here.

After having solved ā€œThe Descentā€ in Python (2) with a plain old for loop and two conditionally updated variables as suggested in the hints, I thought Iā€™d give it another try with functional-style Python 3 and itertools etc.:

:warning: Spoiler warning: I think the code of my attempt should solve the puzzle, even though it fails the tests right now. So if you havenā€™t solved it yourself yet, you might want to not view the code. :warning:

(code hidden, click to expand)
    import sys
    import itertools
    import operator

    # game loop
    while True:
        # see docs.python.org/library/itertools.html?highlight=take#itertools-recipes
        heights = map(int, itertools.islice(sys.stdin, 8))
        mountain_index, mountain_height = max(enumerate(heights), key=operator.itemgetter(1))
        print(mountain_index)

However, when I start the tests, this just gives me

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

Pasting that code into a local file and running it on my computer, I seem to observe the correct behavior.

I thought maybe the import of the additional standard library modules is taking too long, so I added print("script started", file=sys.stderr) right after the sys import (before the other two imports). Though still that timeout message apparently without any debug output from my program.

Whatā€™s going on? Is compiling the script taking too long? Or have I found a bug in the game? Or am I bending the rules too much by changing the template code in places not indicated by the comments?

Hey guys im new to coding and trying to learn python3 anyone got any tips for what phrases to use to find the mountains, i got through test one and it went fine but im having trouble finding a code that will work on all of them.

How does the following code work?

    #include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

/**
 * Auto-generated code below aims at helping you parse
 * the standard input according to the problem statement.
 **/
int main()
{

    // game loop
    while (1) {
        int max = 0;
        int imax = 0;
        
        for (int i = 0; i < 8; i++) {
            int mountainH; // represents the height of one mountain, from 9 to 0. Mountain heights are provided from left to right.
            cin >> mountainH; cin.ignore();
            
            if (mountainH > max) {
                max = mountainH;
                imax = i;
            }
        }

        cout << imax << endl;
    }
} 

When does the while loop get a False output to stop the loop? Also, when loop is re-run, arenā€™t the variables re-created and lose all previous data? Doesnt the for loop restart from i = 0. How is the imax value updating taking place then?

When does the while loop get a False output to stop the loop?

The loop is stopped by the referee program (CodinGame) whenever it encounters a valid condition to stop the puzzle:

  • count of turns has reached its max value for the puzzle
  • victory/defeat condition for the puzzle. Here: all mountains are destroyed (victory) / the ship crashes on a mountain (defeat)

Also, when loop is re-run, arenā€™t the variables re-created and lose all previous data?

Yes. For now (and most of the puzzles/games), you donā€™t need to save data from one turn to another. The referee program computes the new input according to your output, and feeds it to you.

Doesnā€™t the for loop restart from i = 0. How is the imax value updating taking place then?

The mountain with max height can change from one turn to another (while loop) so you need to reset this variable to search for the new highest mountain. The variable serves this purpose in the for-loop.

I hope it helps.

If someoneā€™s stuck with this puzzle - you need the highest mountain, just the first one :smiley:
Did a loop, that checked all the mountains and it didnā€™t work.

c++ the decent right code

A decent right code? Whatā€™s that?

Hi, I am a beginner. :slight_smile: Can you explain to me if all the heights are read at once in the input or every new time? Are they stored as a list, array, something with their i index? If they are single values erased in the next iteration, how do we compare again? I am confused, thanks.

All the code does is read the inputs and provide a (wrong) output. Storing them, running comparisons on them, and generating the correct output is something you have to write code for. This is the challenge.

1 Like

so you actually changed something you shouldnā€™t i guess which is : ā€˜ā€™ for (int i = 0; i < 8; i++)
so the i = 0 should be 8 maybe

i am totally new in programming. can you help what is wrong with this?

class Player
{
    static void Main(string[] args)
    {

        // game loop
        while (true)
        {
            int max = 0;
            int imax = 0;
            max = 0;
            for (int i = 0; i < 8; i++)
            {
        
                int mountainH = int.Parse(Console.ReadLine()); // represents the height of one mountain.
                if (mountainH > max)
                {
                    mountainH = max;
                    imax = i;
                }
                    
            }              
        
            
            
            // Write an action using Console.WriteLine()
            // To debug: Console.Error.WriteLine("Debug messages...");
Console.WriteLine(imax);
                        // The index of the mountain to fire on.

        }
    }
}

it is obvious that replacing mountainH by max is not the best way to check the highest mountain :wink:

yes, thatā€™s it :sweat_smile: , thank you

I wanna know why I canā€™t use ā€˜reverseā€™ and ā€˜sortā€™ in STL with this problem.
Just like this:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

/**
 * The while loop represents the game.
 * Each iteration represents a turn of the game
 * where you are given inputs (the heights of the mountains)
 * and where you have to print an output (the index of the mountain to fire on)
 * The inputs you are given are automatically updated according to your last actions.
 **/

int main()
{
    int a[8];
    // game loop
    while (1) {
        for (int i = 0; i < 8; i++) {
            int mountainH; // represents the height of one mountain.
            cin >> mountainH; cin.ignore();
            a[i]=mountainH;
        }
    sort(&a[0],&a[8]);
    reverse(&a[0],&a[8]);
    for(int i=0;i<8;i++)
    {
        cout<<a[i]<<endl;
    }
        // Write an action using cout. DON'T FORGET THE "<< endl"
        // To debug: cerr << "Debug messages..." << endl;

         // The index of the mountain to fire on.
    }
    return 0;
}

There doesnā€™t seem to be any issue with the usage of sort/reverse as such.
In this problem, you are expected to return only one value at the end of each while loop iteration, which represents the index of the highest mountain. You seem to be returning the entire list of mountain heights in descending order. Please revisit the same. All the Best !! :slightly_smiling_face:

[no full solution here please]