The Descent - Puzzle discussion

Hi, can you tell me, what is wrong?

int hight = 0;
	int highest = 0;
	int highestindex = 0;

int main()
{

    // game loop
    while (1) {
        for (int i = 0; i < 8; i++) {
        if (hight > highest){
				   highestindex = i;
				   highest = hight;}
            cin >> hight; cin.ignore();
        }

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

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

i wonā€™t spoil giving you the answer but only clues.

2 huge errors in your solution :

  • you are supposed to get mountainā€™s height before comparison.
  • you should take care that your variables have good values at each loop
2 Likes

Hey guys,

Iā€™m new to this site.

I read a few tutorials online for javascript beginners (functions, loops, arraysā€¦ basic stuff), wanted to try some exercices to train before I go any further. So here I am andā€¦ Iā€™m getting so frustrated because we canā€™t see the solutions.

I read the exercice and Iā€™m lostā€¦ I have no idea what to do.

After spending 20 minutes on scratching my head I finaly saw the ā€œhintsā€ on the leftā€¦ they talk about stdin stdout stderrā€¦ Are we suppose to use these words in our solution ? (How Iā€™m I suppose to know these? Iā€™ve never heard of them before :frowning: )

I read the algorithm & pseudo-code which I believe is the ā€œsolutionā€ but I have to write it down with proper code and I donā€™t even know where I have to write itā€¦ Is it inside the for that is already there ? or make a new one underneath ?

I tried doing things with typeof, tostringā€¦ When I write these down outside the for it says undefined (I guess itā€™s because of the ā€œscopeā€ ?) when I write it down inside the for itā€™s fine.

I am a complete begginer, even with the last hint I canā€™t understand what to do. If you guys got any hint for meā€¦ <3

(trying to do it in javascript)

Edit : Ok so I checked the answers above to get some answers, saw that I had to write the code around whatā€™s already writtenā€¦ managed to complete the exercice but with no joy because I didnā€™t understand it on my ownā€¦ feels Weird :confused: will try other exercises. <3

Hi! And welcome to CodinGame :slight_smile:
Sorry that youā€™ve had a hard time. The first learning step here is the harder.

Maybe you can point me at what isnā€™t clear in the hints? I have modified them recently and Iā€™m looking for feedback.

image

Why doesnā€™t this work?

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

    // game loop
    while (true)
    {
        int max = 1000;
        int number = 0;
        for (int i = 0; i < 8; i++)
        {
            int mountainH = int.Parse(Console.ReadLine()); // represents the height of one mountain.
            if (max > mountainH) {
                max = mountainH;
                number = i;
            }
        }
        Console.WriteLine(number);
    }
}

}

if max is greater that mountainā€™s height ā€¦ so you choosed to destroy the lowest mountain ā€¦?

TRY THIS :wink:

[no full solution please]

Hello, my Code does not work and I dont know why.:

import sys
import math




while True:
    
    moutain_h = 0
    highest_mountain_h = 0
    highest_mountain_index = 0
    
    for i in range(8):
        mountain_h = int(input())  # represents the height of one mountain.
        if mountain_h > highest_mountain_h :
            highest_mountain_index = i
            Highest_mountain_h = mountain_h
  
    print(i)

Maybe if you print the right variable it will work
:rofl:

Is it me or is this game ā€œAPIā€ is nonsensical ?

Why doesnā€™t this work?

int main()
{
int max;
// game loop
while (1) {
    max = 0;
        
    for(int i = 0; i < 8; i++){
        int mountainH;
        count >> mountainH; cin.ignore();
        if(mountainH > max){
            max = i;
        }
        
        
    }
    // Write an action using cout. DON'T FORGET THE "<< endl"
    // To debug: cerr << "Debug messages..." << endl;
    
    cout <<  to_string(max)  << endl; // The index of the mountain to fire on.
}

}

Because in your code, max is an index AND a height.

1 Like

Why this doesnā€™t work? i think thereā€™s a bug

 import java.util.*;
import java.io.*;
import java.math.*;

/**
 * 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.
 **/
class Player {

    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);

        // game loop
        while (true) {
            int max=0;
            int index=0;
            for (int i = 0; i < 8; i++) {
              int mountainH = in.nextInt();
                System.err.println("mountainH est"+ mountainH);
                if (mountainH==9)
                {
               // removing this if block makes it work     
                    index=i;
                    System.err.println("breaking");
                    break;
                }
                if (mountainH>max) {
                max= mountainH;
                index=i;
                }    
                
                }
                
            System.err.println("broke out");
               

            // Write an action using System.out.println()
            // To debug: System.err.println("Debug messages...");

            System.out.println(index); // The index of the mountain to fire on.
        }
    }
}

i remove the part of comparing the mountainH to 9 it works

I find this extremely confusing. It took me long enough to figure out why the game loop wasnā€™t happening at a standard framerate of between 30, 60 or 100 times per second. When I finally figured out ā€“ or did I? ā€“ what a ā€œturnā€ is supposed to mean, I wrote this code:

while (true)
    {
         List<int> mountains = new List<int>();
         for (int i = 0; i < 8; i++)
         {
             int mountainH = int.Parse(Console.ReadLine()); // represents the height of one mountain.
             mountains.Add(mountainH);  
         }
        mountains.Sort();
       Console.Error.Write("sorted" + mountains.Count + "," + mountains[mountains.Count-1]);

        Console.WriteLine(mountains.Count-1); // The index of the mountain to fire on.
      

    }

It does not work. It never shoots down mountain of height 9, despite the fact that said mountain is lying right at mountains[mountains.Count-1] after I sort it. I think thereā€™s a bug somewhere in your engine code thatā€™s ignoring the new, sorted indices and using some fictional indices that we canā€™t see under the hood.
(as a side note, I did clear the mountains list between ā€œturnsā€ initially as well, but with the sort and the weirdly infrequent iteration of the while loop, itā€™s pointless)

Of course it does not workā€¦

Your code will always print the number of moutains-1 ā€¦ it is not the position of highest mountain !!!

List in not a good datatype for your algorithm.
Moreover you donā€™t really need to do like this (there is a more simple solution)

Did you not see my sort command? By the unusual logic of writing the indices to destroy mountains, the last index of that list should and does always have the highest mountain. Also, with the loop being called once per ā€œturnā€, it doesnā€™t matter. I was trying all options to see how malleable the interface was.

Yes i have seen your ā€œsortā€ ā€¦ but have you read your code ?

You are just printing ā€œmountain.Count-1ā€ ā€¦ which wil always return 7 ā€¦

Have you? Writing the index of the highest mountain is what youā€™re supposed to do. When I sort them, the index of the highest mountain is always 7. I even checked that with the debugger.

Look, Iā€™m sorry to be blunt to the point of being typical ornery software engineer-ish, but at the very least, there is a fundamental communication problem with the way these puzzles are described to the end user. There is zero explanation of where these magic indices come from, where they are stored, why the indices I use in my list donā€™t map to them properly, or what data connects Console.Writeline(index) to the actual mountain.

This would be an absurdly simple problem to solve in real C#, in a real Unity environment, yet in this venue itā€™s giving me, an admittedly dense in certain ways, but still experienced programmer, a lot of trouble. Itā€™s making up weird rules and hiding code, and IMO, nothing good can come from that in the long game. In a framerate loop, I would not use a sort for each frame (although the default comparer is Quicksort, which is good), but a test environment like this should be able to understand what Iā€™m trying to do.

hello and welcome to CodinGame @MichaelGreenhut

I totally understand you get confused on your first steps on the platform while being an experienced programmer.

Have you, by any chance, seen the hints on the top left menu?

Now, about your code:

  • if you sort a list of integers, the index of the highest integer will always be the last one (size - 1). However, the puzzle asks you the index of the highest mountain as they are given to you, not after you sort them.

Does it help? Iā€™ll be happy to answer any question you might have.

i need help