The Descent - Puzzle discussion

You are supposed to dynamically find the index in each turn instead of pre-computing all answers (hardcoding), and you are supposed to output only one index per turn.

I’m not understanding this one bit. Isn’t input() supposed to be input by a user? Why is it empty? If the mountain_h is randomly generated, why don’t we need import random and .randint() and all that? Why does it say print(i) is wrong (my solution for the first round) when it just goes down the list of highest to shortest mountains (it gets the job done but won’t work for later things)? Why not? Why does the final print statement say print(“4”) when the mountain_h is clearly turned into an int()? Why does it make the print value a string? Am I the only person with all these questions??

See if this answers your questions:
https://www.codingame.com/playgrounds/40701/help-center/how-does-it-work

Hi

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

with this code and I wonder why.

Thanks

class Player {

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

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

        // Write an action using System.out.println()
        // To debug: System.err.println("Debug messages...");
        System.out.print(imax); // The index of the mountain to fire on.
    }
}

}

You need to output using println instead of print (remember to read the remarks in the default code :wink: ).

By the way, please specify what language you are using, and format your code properly by using the </> button in the formatting toolbar. Also note that except for a few beginners’ puzzles here, posting the full/core code here is not allowed.

1 Like

hello there!!

so I figured out that you have to input the mountain number to fire on, but I just cant understand. how do I
put multiple numbers so i can fire on multiple mountains? because right now I can only fire on one.

thanks so much for your help bye the way!

Each round you are given 8 heights, and you are limited to fire on the highest one only. If you’re successful in one round, you’ll have another round to fire on another mountain again. In other words, you’ll always be given enough rounds to eventually fire on all the mountains if you don’t fail any rounds.

Error report in C:

Code:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>

//This code works, but the website is broken.

int main()
{
while (1)
{
int max=0, ans=0;
for (int i = 0; i < 8; i++)
{
int mountain_h;
scanf(“%d”, &mountain_h);
if (mountain_h>max)
{
max=mountain_h;
ans = i;
}
}
printf(“%d”, ans);
}
return 0;
}

in the terminal I get (for test case 3):

Sortie standard :

5

Informations :

Timeout: your program did not write to the standard output in due time. Height of mountain 0 : 0 Height of mountain 1 : 6 Height of mountain 2 : 7 Height of mountain 3 : 5 Height of mountain 4 : 0 Height of mountain 5 : 8 Height of mountain 6 : 1 Height of mountain 7 : 0

I get this for every test even though I always print the right answer.

please fix.

Actually, your code works. It just needs some fixes:

scanf("%d", &mountain_h);

and

printf("%d\n", ans);

Hello
Using Python
Can someone correct me ?
The first Test case is PASS
but all other FAIL

    I_max = 0
    Max_h = 8
    for i in range(8):
        mountain_h = int(input())  
        if mountain_h >= I_max or mountain_h <= I_max:
            print(I_max)
            I_max += 1
            
    

also what dos
“your code did not read all available input before printing an instruction, your outputs will not be synchronized with the game’s turns and unexpected behaviour may occur.”
mean?

Thenks!

Please edit your message and format your code using the </> button in the formatting toolbar. Without the proper indentation it’s difficult to read and check your code.

2 Likes

The message means your code has output too soon.

In this puzzle, there are multiple rounds. In each round, you’re supposed to read all the 8 heights first, before you’re able to tell which mountain is the highest and the associated index.

However, your code may output something before you finish reading all the heights. That’s why your code may fail to correctly identify the highest mountain, and that’s also why you get that message.

1 Like

What did I do wrong?

import sys
import 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.
mountain_h = []
LoopCount = 0
Order = []
# game loop
while True:
    for i in range(8):
        mountain_h = list(input())  # represents the height of one mountain.


    mountain_h_sorted = mountain_h.copy()

    mountain_h_sorted = mountain_h_sorted.sort


    for i in mountain_h:
        Order = list(mountain_h.index(mountain_h_sorted.pop))


    ShootingOrder = tuple(Order)

    Shoot = ShootingOrder.index(LoopCount)
    LoopCount =+ 1
    # Write an action using print
    # To debug: print("Debug messages...", file=sys.stderr, flush=True)
    # The index of the mountain to fire on.
    print(Shoot)

mountain_h = list(input()) means you read the height of a single mountain and create a list out of that single height, and you do that 8 times via the for loop. You should be reading 8 heights into one list instead.

1 Like

That helped a lot! Here is my final code.
Thanks again! :smiley:

[Mod edit: No full code please]

For test case 2 - if the first mountain height is 9 then we never enter the if statement again… max will always be 9 and maxI will always be 0 … I’m so confused…

How is it printing outside the for loop too

I’m a bit confused… Are you asking about your code or somebody else’s code here?

It should be the highest mountain? I don’t understand


mountains = []

game loop

while True:
for i in range(8):
mountain_h = int(input()) # represents the height of one mountain.
done = 0
if( len(mountains) == 0):
mountains.append([mountain_h,i])
done = 1
h = mountains[len(mountains)-1][0]
if( h > mountain_h) :
mountains.append([mountain_h,i])
else :
for k in range( len(mountains)-1, -1, -1):
if( done == 0 and h > mountains[k][0] ):
mountains.insert(k , [mountain_h,i])
done = 1

print (mountains[0][1]) # heighest mountain??

With your code, the heights read for all rounds will keep getting appended to the list “mountain”. But actually, in each round you’re given all the heights of the remaining mountains for that round, so you would want to empty the list at the beginning of each round.

By the way, you may format your code on this forum properly by using the </> button on the formatting toolbar. Otherwise, it is difficult to read your code without the necessary indentation.

Thanks a lot

So I tried to clear the mountains list. First turn is working, than it seems it doesn’t read the inputs, (very weird output as I tried to print) the mountains are not updated

mountains = []
iloop = 0
while True:
    mountains.clear()
    for i in range(8):
        mountain_h = int(input())  # represents the height of one mountain.
        done = 0
        if( len(mountains) == 0):
            mountains.append([mountain_h,i])
            done = 1
        h = mountains[len(mountains)-1][0]
        if( h > mountain_h) :
            mountains.append([mountain_h,i])
        else :
            for k in range( len(mountains)-1, -1, -1):
                if( done == 0 and h > mountains[k][0] ):
                    mountains.insert(k , [mountain_h,i])
                    done = 1
    print (mountains[0][1]) # heighest mountain??