The Descent - Puzzle discussion

What is the problem with my code ?
int main()
{

 int target=0;
 int highest=0;
// game loop
while (1) {
    for (int i = 0; i < 8; i++) 
    {
        int mountainH;// represents the height of one mountain.
        scanf("%d", &mountainH);
       
        […]
    }
    fprintf(target,"target \n"); 
}

return 0;

}

Why do you print the word “target”?

there is no bug, its my mistake.

For the “descent” coding in C. I waste a few time because printf("% \n,imax) instead of printf(%d\n",imax) ! The white space after the (correct) integer value cause an error message .

Not really sure why this isn’t working for Python 3:

while True:
for i in range(8):
hmax = 0
imax = 0
mountain_h = int(input()) # represents the height of one mountain.
if mountain_h > hmax:
hmax = mountain_h
imax = i
# Write an action using print
# To debug: print(“Debug messages…”, file=sys.stderr)

# The index of the mountain to fire on.
print(imax)

It’s also the solution code…

Just move hmax and imax outside of the loop…

So i’m C++ in the descent and I can’t understand what i’m doing wrong honestly. here is my loop code
while (2) {
for (int i + 0; i < 8; i++) {
int mountainH;
cin << mountainH; cin.ignore();
}

 cout << "1" << end1;

What is it that i’m doing wrong?

I have a logic question for C++. I figured out the solution through trial and error, but I’m confused why the solutions works. It’s a variation of the Solution offered.

`for (int i = 0; i < 8; i++){
      int mountainH;
      cin >> mountainH; cin.ignore();
      if (mountainH > max){
          max = mountainH;
         /* How does this work? If it looks at the highest mountain,
            the second time shouldn't do anything because 8 < 9.
            But if it resets to 0, why doesn't it just do the mountains descending
            order of "i" and fail at the other tests?*/
          imax = i;
      }
 }`

Thank you for your patience. I don’t want to just code. I want to understand why things work.

I need help understanding python loops. I managed to complete the first test case, but when it came to the second one, another problem rose. How should i go about this? I used the same format that i used for the first case, but it didn’t seem to recognize it. I used the break so the code wouldn’t be continuous, but it still kept going anyway. Here’s the format i used. I’ve been told it looks like i’m hardcoding.

while True:
    max = 0
    imax = 0
    for i in range (8):
        mountain_h = int(input())
        if mountain_h > max:
            max = mountain_h
            imax = i 
    print("0")
    print("1")
    print("2")
    print("3")
    print("4")
    print("5")
    print("6")
    print("7")

What am I doing wrong?

The puzzle is a turn-based game. It means that you can output only one action (with print) by turn (an iteration of the while loop). Each turn you shoot a mountain but your ship also goes down, hence you have to shoot the highest mountain to be sure not to crash.

Hey,
I’ve managed to complete the code for testcase 1, but I’m still struggling with scattered mountains. My ship gets destroyey at mountain 4.
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);
    int hmax = 0;
    int imax = 0;

     // game loop
     while (true) {
         for (int i = 0; i < 8; i++) {
             int mountainH = in.nextInt(); // represents the height of one mountain.
             if(mountainH > hmax) {
                 hmax = mountainH;
                 imax = i;                    
             }
         else if (mountainH < hmax) {
             hmax++;
             imax++;
         }
        
          System.out.println(imax);
         }
         
         // Write an action using System.out.println()
         // To debug: System.err.println("Debug messages...");
    
     }
    

    }
    }

     				Game information:
     				Let's destroy those mountains to secure our landing...
    

Height of mountain 0 : 9
Height of mountain 1 : 8
Height of mountain 2 : 7
Height of mountain 3 : 3
Height of mountain 4 : 6
Height of mountain 5 : 5
Height of mountain 6 : 2
Height of mountain 7 : 4

					16
				
			
			
		
			
				
				
				
					Standard Output Stream:
					0
				
				
					Game information:
					Your spaceship targeted mountain 0, your altitude is now 9

Height of mountain 0 : 0
Height of mountain 1 : 8
Height of mountain 2 : 7
Height of mountain 3 : 3
Height of mountain 4 : 6
Height of mountain 5 : 5
Height of mountain 6 : 2
Height of mountain 7 : 4

					26
				
			
			
		
			
				
				
				
					Standard Output Stream:
					1
				
				
					Game information:
					Your spaceship targeted mountain 1, your altitude is now 8

BOOOOMM!! Nice Shot!

Height of mountain 0 : 0
Height of mountain 1 : 0
Height of mountain 2 : 7
Height of mountain 3 : 3
Height of mountain 4 : 6
Height of mountain 5 : 5
Height of mountain 6 : 2
Height of mountain 7 : 4

					36
				
			
			
		
			
				
				
				
					Standard Output Stream:
					2
				
				
					Game information:
					Your spaceship targeted mountain 2, your altitude is now 7

Height of mountain 0 : 0
Height of mountain 1 : 0
Height of mountain 2 : 0
Height of mountain 3 : 3
Height of mountain 4 : 6
Height of mountain 5 : 5
Height of mountain 6 : 2
Height of mountain 7 : 4

					46
				
			
			
		
			
				
				
				
					Standard Output Stream:
					3
				
				
					Game information:
					Your spaceship targeted mountain 3, your altitude is now 6

Height of mountain 0 : 0
Height of mountain 1 : 0
Height of mountain 2 : 0
Height of mountain 3 : 0
Height of mountain 4 : 6
Height of mountain 5 : 5
Height of mountain 6 : 2
Height of mountain 7 : 4

					56
				
			
			
		
			
				
				
				
					Standard Output Stream:
					4
				
				
					Game information:
					You crashed your spaceship on mountain 4

Height of mountain 0 : 0
Height of mountain 1 : 0
Height of mountain 2 : 0
Height of mountain 3 : 0
Height of mountain 4 : 6
Height of mountain 5 : 5
Height of mountain 6 : 2
Height of mountain 7 : 4

If someone could help me, I would be really appreciate it.

What’s the points of your ‘else if’

Well, if mountainH is less than hmax, it should adjust hmax and imax so that my spaceship can surpass those mountains while shooting on them. This code also worked for the first task “descending mountains”, but it doesn’t work for 2. So I guess, I need to adjust something.

I’m pretty sure your code without the else if statement, pass the first one too.
Also you can’t print in the for-loop, you have to print only 1 times per turn (while-loop)

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[]) {
    int hmax;
    int imax;

     Scanner in = new Scanner(System.in);
    
     // game loop
     while (true) {
        
         for (int i = 0; i < 8; i++) {
             hmax=0;
             imax=0;
             int mountainH = in.nextInt(); // represents the height of one mountain.
         }
         if(mountainH > hmax) {
             hmax = mountainH;
             imax = i;
         }
         // Write an action using System.out.println()
         // To debug: System.err.println("Debug messages...");
         System.out.println("imax");
         // The index of the mountain to fire on.
         System.err.println("Debug messages");
     }
    

    }
    }

Now I get the error: error: cannot find symbol

        if(mountainH > hmax) {
           ^

Also what exactly do you mean by saying that you can’t print out in a for-loop? Does that mean I should implement “Syso” into while-loop after braceleft?

If you print in the for-loop going from 0 to 8, each turn.

Turn 1: You will print 8 times, so the game will have data for the next 8 turns !
Turn 2: Same and the server can already play 15 turns without any data.

So yes, you have to print only one time in the while-loop ( a turn)

I think I am stuck somehow and don’t know how to get out of it. I still get the error: “cannot find symbol” and it refers to “if(mountainH > hmax) {^”.

Maybe you know a solution for it? I didn’t change the code until my last post.

In Java, the scope of the variable is block. That means variables are live (and accessible) only in their block ‘{’ and ‘}’

int mountainH = in.nextInt(); is define in the for-loop which is good and that means mountainH is a local variable and only accessible in the for-loop (under the definition btw)

But, you try to use it outside the for-loop so your program crash, try to move the if statement in the for-loop or use a global variable.

Now, it works, but I had to print every mountainH going from 0 - 7.
Thanks for your help, I guess it’ll work kinda the same in part 2. I only might change the type of mountain my ship should shoot on.

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[]) {
    int hmax=0;
    int imax=0;

     Scanner in = new Scanner(System.in);
    
     // game loop
     while (true) {
        
         for (int i = 0; i < 8; i++) {
             
             int mountainH = in.nextInt(); // represents the height of one mountain.
             
             if(mountainH > hmax) {
                 
             hmax = mountainH;
             imax = i;
             
         }
         }
         // Write an action using Syste System.out.println("0");m.out.println()
         // To debug: System.err.println("Debug messages...");
         System.out.println("0");
          System.out.println("1");
           System.out.println("2");
           System.out.println("3");
           System.out.println("4");
           System.out.println("5");
           System.out.println("6");
           System.out.println("7");
         // The index of the mountain to fire on.
     }
    

    }
    }

You HAVE TO PRINT ONLY ONE TIME PER TURN. :rage:

  • What’s the point of your if statement which define imax, if you don’t use it in your print statement…
  • You have to reset hmax each turn otherwise it will keep focusing the same mountain forever.
1 Like