The Descent - Puzzle discussion

The hints mention “stdin” and “stdout” but these are nowhere to be found in the code. What’s up with that?

Stdin is the standard input and stdout the standard output, they are relatives to the system, not the code, so the functions used to read on stdin and write to stdout depends of your programing language. But they are presents in the default code.

1 Like

I’m using JavaScript. What’s my stdin and out in that case?

If you look to the default code, you have the readline() function that read on stdin, and the console.log() function that write on stdout.

1 Like

You can also use print() instead of console.log().

Hello this is kind of my first problem on CodinGame, it’s a great platform seems like but I don’t get something, does the IDE work a lot different than a normal IDE/editor? Because for example the while(True) should never stop right? Yet it does? I’m working on Python btw.

Sorry for the stupid question but I don’t feel quite at home working on this problem.

did you notice the “hints” section?
image

You can notice a infinite while loop in the default code. It’s the game loop.
Each iteration of the while loop corresponds to a turn of the game where your code must read input data in stdin and output a response in stdout.
The game loop is actually not infinite: there is a max number of turns allowed to solve each puzzle. Once your program sends the expected output or reached the expected state, the referee program (on CodinGame side) will end the loop.
It’s really important to continue reading all input data on the stdin each turn; else, your program will desynchronize from the referee program.
With the same idea, if you start reading the input data of turn n+1 on the stdin without having sent a response in the stdout at turn n, the referee program will consider that your program did not respond in due time (timeout).

I infact did not :sweat_smile:, thank you so much for the swift reply!

why this works :

loop do
    max = 0
    index_max = 0
    8.times do |i|
        mountain_h = gets.to_i
        if mountain_h > max
            max = mountain_h
            index_max = i
        end
    end
    puts index_max
end

And this doesn’t :

loop do
    max = 0
    index_max = 0
    8.times do |i|
        if gets.to_i > max
            max = gets.to_i
            index_max = i
        end
    end
    puts index_max
end

It gives me

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

I’m new to Codingame, the variables available as inputs are not clear.
The gets variable is not describe enought, I had to figure how it works with :

8.time do |i|
   STDERR.puts "i:gets --> #{i}:#{gets}"
end

After 5 years in ruby, I think this is not a good exercice for beginers, as there is hidden behavior not documented.

The comments should have been this :

the variable ‘gets’ is your only input. It represents the height of the current mountain.
it’s value evolve in the 8.time loop, and represent the height of the next mountain for each loop.

gets is not a variable. It’s a function, that reads a given input and writes it to example in a variable:

mountain_h = gets.to_i

This takes an input (in this case the height of a mountain), converts it to an integer and writes it to the variable mountain_h .

This is why your second solution timed out. You use gets twice instead of storing the result in a variable. This means that your program will try to read 16 inputs, but only 8 are given. Your program waits until the next input comes and times out after a few seconds.

If you are new to codingame, I would recommend to take a look into the “Hints”-Section on the “Descent”-puzzle. There is a good explanation of how input and output work.

1 Like

Ok thx for the answer :pray:

Even if it’s a function, my message stay the same :wink:

Ok so each time “gets” is read, the value of “gets” evolve ?
If it’s the case, it’s a shame :stuck_out_tongue: because the behavior of the function is not explain, so the challenge is not the exercice but it is to find how to use the tool.

I read the hints several times, sorry mate it still wasn’t clear about “gets”.

The interface of codingame is awesome, I like it ! But Sorry I’m a bit decieved by the challenge, because it is the code outside the challenge who is challenging :wink:

“gets” is a standard ruby function which read from the standard input… Is the statement needs to be a copy of the ruby’s documentation ?

2 Likes

@Vrizzt I completely understand the frustration of not understanding how CodinGame works with the standard streams, especially when you’ve never used them before. It was my case when I started CG and I was lost.

Fortunately, we have had the language assistance in the IDE for 2 or 3 months now:

1 Like

Thx for your time :slight_smile:

The problem for me was not about the use of gets, but what’s defined as the input for gets. The comments, for a good understanding, could be improved, because I think the challenge is not about guessing the arguments given to gets.

When I tried to resolve the exercise, It took me time to find what was behind gets.

May be I’m missing something, I still have a lot to learn :wink:

You speak of the comments… But have you noticed the entire part of the statement dedicated to the inputs and outputs description on the left of the IDE ? Have you seen that you can scroll down ?

Yes I read them, the UI is pretty well design so I found it quickly.

ItsAFeature already asked me the question :

I would recommend to take a look into the “Hints”-Section on the “Descent”-puzzle. There is a good explanation of how input and output work.

And yes it written everywere that the input is on STDIN, but that’s all.

I scrolled down the description of the game on the left, and its written :

Inputs for a game round
8 lines: an integer mountainH per line. It represents the height of a mountain. The heights of the mountains are given in the order of their index (from 0 to 7).

8 lines -> one mountainH per line. I didn’t understand that. What is a line ? A line on a loop in loop.do or a line in 8.times ? When I saw :

loop do
    8.times do
        mountain_h = gets.to_i
    end
end

I wondered what was behind gets and what was the ouput of the function through the code. That’s why I first try :

loop do
  max = 0
  index_max = 0
  8.times do |i|
    if gets.to_i > max
        max = gets.to_i
        index_max = i
    end
  end
  puts index_max
end

That’s all. May be I missed something, I prefer to be carefull :sweat_smile:

The discussion is neverending, the subject I’m trying to explain is not so important, I just wanted to understand the behavior of the function “gets” because I had to dig manually the output of the function to find out how to use it. 'Cause the data given to STDIN is not described.

My opinion is just to add info about that to help users to use gets easly and focus on the challenge. I think the challenge could be improved that way, It doesn’t prevent me to admire Codingame :wink:

It just feel like the comment is written by someone used to the way codingame is made, and some shortcut are made in the comments.

It’s just my opinion, it may be true just for me as a new user of codingame, I wanted to be as constructive as possible.

1 Like

It could be a never-ending discussion. But as you are very open in explaining your difficulty, I’m just curious wishing to understand in what ways or how or why new learners are feeling difficult in the learning.

As you said the difficulty is in understanding gets, or stdin
I could understand your difficulty because the IDE hid you from the console. You do not have a “console experience”.

To reveal what is behind the scene, the following 8 lines could be an example of the input set in one turn. This is just an example and the numbers can be ordered randomly and will be different in every game turn.

1
2
3
4
5
6
7
8

In every game turn, you (your program) will got 8 lines of numbers as input. Every time when you call gets, you will get one line (one number) from the input. The number will be stored in a variable mountain_h in the sample code. You need to save the value to keep it (often save to an array, so that you should define an array before going into the loop) or consume it immediately (need to define some variables like max before the loop to keep track of progress), otherwise the mountain_h value will be overwritten in the next call to gets.

There are 8 lines so that you should exactly call gets 8 times to receive all 8 numbers. Your code called gets 8 * 2 = 16 times so that it is incorrect.

Please let me know does this explanation make sense to you? If not, in what way?

Assume you are the writer of the statement, what would you write in the “input” section or other sections so that it will be clear enough for yourself and for most others? Show us your improved version. It can be a highly valuable contribution from you.

Thanks.

1 Like

The Scala-solution in the hints is not working:

1 Like

thanks! It’s fixed.

1 Like

What am I even supposed to do here? I can’t find any instructions?