The Descent - Puzzle discussion

class Player
{
static void Main(string[] args)
{
//List lst = new List();
// game loop
int j=-1;
int k=0;
while (true)
{
for (int i = 0; i < 8; i++)
{
int mountainH;
mountainH = Convert.ToInt32(Console.ReadLine()); // represents the height of one mountain.
if(mountainH>j)
{
j=mountainH;
k=i;
}
}
//Console.Error.WriteLine();
// Write an action using Console.WriteLine()
// To debug: Console.Error.WriteLine(“Debug messages…”);
//int j= lst.Max();
Console.WriteLine(k); // The index of the mountain to fire on.
}

}

}

you were right but now after changing the code its everytime it is giving index as 0 but in my visual studio it is giving right index

Because you never resetting j after a shoot.

i m resetting the values see if(mountainH>j)
{
j=mountainH;
k=i;
}

its working fine in my visual studio

You’re assigning it there, you don’t reset it after a shoot, so j is always the height of the highest mountain of the 1st turn.

If it’s work on your Visual Studio, it’s because your Visual Studio is broken too.

Firstly I would like to say thanks to the devs of this site, this is a great idea to learn programming and good fun. Im looking forward to progressing through the challenges and trying different languages.

With this first puzzle i thought i got it sorted until i got to “String Mountains 2”. For some reason when my ship is on the 3rd line down and fires upon the mountain close to the edge of the screen it then returns on the next line and continues to fire on the same mountain.

I’m not sure if this is a deliberate problem for me to solve or a small glitch. I suppose if the mountains are randomly generated in size then the next time i logon it may work.
I have included my code below, any pointers or advise much appreciated:

Below code is in C#

class Player
{
    static void Main(string[] args)
    {
        int[]myArray={0,1,2,3,4,5,6,7};//array to contain mountain heights
        // game loop
        while (true)
        {
            for (int i = 0; i < 8; i++)
            {
                int mountainH = int.Parse(Console.ReadLine()); // represents the height of one mountain.
                myArray[i] = mountainH;//assign mountain heights to array
            }

            // Write an action using Console.WriteLine()
            for (int i = 0; i < 8; i++) //loop through mountains
            {   
                if (myArray[i] == myArray.Max()) //if current mountain has max value 
                {
                    Console.WriteLine(i.ToString());//Fire
                }

            }
            // To debug: Console.Error.WriteLine("Debug messages...");
        Console.Error.WriteLine("Exceeded Parameters");
            //Console.WriteLine("4"); // The index of the mountain to fire on.
        }
    }}
1 Like

This one is a bit subtle but think it through. The program asks for one shot per turn (one turn being one run through the while(true) loop). Is that what your program is providing?

1 Like

Cheers MrA,
I will take another look at this tonight :slight_smile:

#1 - Where is Input() getting its values from?
for i in range(8):
mountain_h = int(input()) “”“Where is this input() coming from?”""

Can someone explain to be how this loop is getting its input()? My understanding has always been that input() comes from the user. When the user does X thing. But in this situation there is no user doing X thing, there is just a loop iterating through each of the items within its range. I’ve tried to go through the documentation to understand how input() and range() can be used, but there is not a single tutorial I could find that walks through or explains how this is possible.

Where is the input() coming from; can someone link to a tutorial that describes the rules for how this input() without a user works? I’m just very confused as I would have never figured this puzzle out because I would have never known it was possible to do input() and yet a lot of people are able to do it so I must be the only person who can’t grasp what seems obvious to everyone else :-/.

#2 - Are there predefined Variables or does only the output matter in print()?
I’m also a little confused because I’m not sure how the computer knows what variables I’m supposed to use. Like when I first sat down to do this I couldn’t figure out what the computer wanted me to use – or does it not care what variables I use only on the final output?

Welcome to CodinGame!

  1. Input comes from stdin. For these puzzles, stdin is mapped to a text file rather than the keyboard. input() reads one line (or maybe one character; I don’t know that language) from the text file.
  2. All that matters is what you output and how long it takes the program to generate the output after it gets the input. Time allowed varies by puzzle but is always < 1s and usually more like <100ms. Use whatever variables you need, it doesn’t matter as long as the answer is correct and timely.

Helpful hint: it’s not immediately obvious on some puzzles but you can scroll down in the graphics window to see a detailed description of the problem, the meaning of the inputs, and what it expects the output to look like.

1 Like

Greetings! I'm noob here so could smb say me what's wrong with this one?

class Player
{
    static void Main(string[] args)
    {
        while (true)
        {
            for (int max = 9; max > 0; max--) //mountain size
            {
                for (int i = 0; i < 8; i++) //number
                {
                    int mountainH = int.Parse(Console.ReadLine());
                    if (mountainH == max)
                    {
                        int imax = i;
                    }
                }
                Console.WriteLine(imax);
            }
        }
    }
}

Welcome to Codingame.

imax does not exist outside of the if statement, so you can’t write it to the console outside of it

If this were not the case, you would still fail to give an output if there were no mountains of height 9 on the first turn.

I think I see what you’re trying to do but you need to examine how the loops are nested. Can you eliminate one?

Thanks! I think i got it. i’ve just started learning C#, so your advice was very informative and helpful for me.

Ok, dudes, i’m sorry it’s me again. i’ve made some changes in my code, but implementation takes a lot of time and it returns error. What should i do?

class Player
{
    static void Main(string[] args)
    {
        int imax = 0;
        while (true)
        {
            for (int max = 9; max > 0; max--) //mountain size
            {
               
                for (int i = 0; i < 8; i++) //number
                {
                    int mountainH = int.Parse(Console.ReadLine());
                    if (mountainH == max)
                    {
                        imax = i; 
                        Console.WriteLine(imax);
                    }
                }
            }
        }
    }
}

There are 8 inputs each turn. That’s all you get until you give an output, at which point you get 8 new inputs showing the new state of the puzzle. Console.ReadLine() reads one of those inputs. If no more inputs exist, you get an error.

Knowing that, ask yourself what happens when there are no mountains with height 9 on the first turn.

Hint: some more local variables might help you here.
Hint 2: Console.Error.WriteLine will write to the error channel, which shows up in your viewer but doesn’t count as output. It’s useful for debugging.
Hint 3: You will want to set imax to zero more often.
Hint 4: Lots of good information on approaching this puzzle already exists in this thread.

int main()
{

// game loop
while (1) {
    int max = 0;
    int biggestM = 0;
    for (int i = 0; i < 8; i++) {
        int mountainH; // represents the height of one mountain.
        cin >> mountainH; cin.ignore();
    //biggestM = mountainH;
    if (mountainH>=max)
    {
   biggestM=mountainH;
    max = i;

    cout<<max<<endl; 
  
    
    }
       
    }
   
    // Write an action using cout. DON'T FORGET THE "<< endl"
    // To debug: cerr << "Debug messages..." << endl;

    //cout << "4" << endl; // The index of the mountain to fire on.
}

}

this is my code. I can’t figure out what I do wrong. Why does it stop attacking the mountains near the end? I have no loop brake as far as I know.

Giving this problem a 1 star rating because instructions on solving this isn’t very clear, and there’s no solutions that actually help to solving this. If there were some programming constructs that gives simple hints on what needs to be done, then it be easier to figure out the problem to solve this.

Thought I would try this site being completely new to coding, found this does not help at all in the slightest.
Where should I start to learn coding? a book? a course? most online stuff is directed at new to advanced def not for beginners.
I copied the hint code line for line and still get the ship shooting only the first and highest mountain.
2nd pass and 3rd pass this continues till I crash into 2nd highest mountain.
From what I can read in the text box, my ship does not reaquire the new highest target, how do I find out what im meant to do to solve this problem?

this has a lot of bugs i’m confused as to how to answer this question

Hi, I’m new to programming and I’m doing this puzzle in Python and I’m completely confused on how to do it, ive tried to do as the solution said to do and its still wrong, can someone please help?