Kirk's Quest - The Descent - puzzle discussion (old)

I’m using Ruby and I’m having trouble understanding what mountain_h is. Is it an array? how can I access it? if it’s not an array, do I need to put it into an array? I’m pretty sure I can figure everything else out once I know what mountain_h is.

Next 8 lines: one integer mountainH per line. Each represents the height of one mountain (0 means that the mountain is destroyed). The mountains’ heights are provided from left to right.

ok. I think i’ve got my head around that part but i’m trying to put all of those integers into an array and I keep getting errors. Do I need to place the whole 8.times do… lines into the array [] brackets or am I doing that wrong? I plan to sort the resulting array in descending order and have the ship fire at the highest point, but I need to get the array to work first.

You can use :

  • Use an array, sort the array in descending order and take the first elememt to find the highest, but you will also need the posX of this mountain
  • Use a list/array and find the maximum element
  • Use an if statement in the for loop to find the index/highest mountain

Really frustrating problem… The problem itself is simple. The way to retrieve the input is frustrating. The mountain height variable can only be accessed inside the for loop. The for loop is REQUIRED. Thus I can’t really write my max mountain height algorithm in there since it’ll calculate the max mountain height every step, when it only needs to calculate it once per pass.

As an “easy” level problem, this puzzle could really, REALLY use the option to view the inputs/formats of each test… most of the other puzzles in the easy category do this, and it helps beginners like me focus on how to make the algorithm work, not what the heck the problem is even asking. I get that mountainH is the height listed from left to right… does that mean it’s in an array? Is it a string of numbers? This might be obvious to other, more seasoned coders out there, but as a beginner I have no idea what to do.

This is compounded by the fact that printing mountainH doesn’t actually reveal anything in the console.

Inputs are explained in the statement:

Input for one game turn
(…)
Next 8 lines: one integer mountainH per line. Each represents the height of one mountain (0 means that the mountain is destroyed). The mountains’ heights are provided from left to right.

So mountainH is an integer, and an integer is a single number. If this point was not clear to you, I advise you to search what ‘types’ exist in your language and what do they represent.
If you want to have the heights in an array, you will have to create an array and add elements in it by yourself.

You might also find interesting to read more about ‘reading the standard input stream’, as CodinGame sends you info through the standard input stream. (You can skip this part and just use our template though.

1 Like

Thank you for the clarification and explanation.

I have been doing some reading on standard input stream, and it seems (based on what I can find and my limited understanding) this is not utilized in JavaScript since there is generally no direct terminal input in JS applications. I have found some resources for handling stdin in JS and will work from there.

Thank you again for your help.

I have been doing a lot of research and reading, and I cannot find a way to turn the stdin into an array in JavaScript. With strings of numbers (as provided in other challenges), I can simply use string.Split() method. I cannot find a way to manipulate the integers on multiple lines into an array… nor can I find another way to parse the inputs.

Any additional guidance or help would be appreciated. Thank you again.

If you want to take n things with readline from std-in and put them into an array, you can use a for loop.

var arr = [];
for (var i=0; i<n; i++) {
    arr[i] = parseInt(readline());
}
1 Like

Hi @MayankSachan, I am wondering if you found a solution to your problem? I am also using C and also get only 83% when submitting. When I run all the tests in the Test Panel, i pass all the tests no problem.

Once I submit my code, test 4 suddenly stops working as it should. After destroying the 4th mountain in sequence(mountain 8), my spaceship keeps firing at mountain 8, even though it has been destroyed.

Do you maybe have any words of guidance for a C newbie?

UPDATE : - I managed to score 100%!! :smiley: I found that the position of my highest mountain was not updating after destroying mountain 8(4th in sequence of destruction). The reason why it wouldn’t update is still unknown to me as it works fine for all other tests, but i found that if i reset the position of my highest mountain back to 0 after each game loop/turn, it solves whatever the issue was.

***For a visual representation:***

// Variable declarations
. . .                  // Array of mountains
. . .                  // Highest mountain
int mountainX;  // X-axis position of highest mountain

-- START game-loop --

// Determine highest mountain and X-axis position of highest mountain

// FIRE or HOLD

mountainX = 0; // Reset to 0

-- END game-loop --

This one is really frustrating. the discription is not very helpful.

Edit: Ok, i solved it now. But the description cofused me a lot^^

Python 3.

I’m at a loss on how to know which mountain the ship is over. Is mountain_h a list?

the variable spaceX: the horizontal coordinate of your ship (0 to 7)
(…)
the height of each mountain from left to right (8 values mountainH)

If spaceX = 0, then you are on the left (= first mountainH read). If spaceX = 7, then you are on the right (= last mountainH read).
mountainH is a number, not a list.

Ok, so there are eight values of mountainH. How does one discriminate between one and another? That’s what I can’t understand. If it’s the same variable, how does the game keep track of them if it’s not a list? Or is it a very long string/ integer where each value is the number in a given position ?

Inputs are given from the standard input stream. You do not need to try to understand what is done on this part as long as you follow the default generated code that read that stream correctly.

mountainH is read in a for loop that follow the pseudo code:

for 0 to 7
    mountainH <- read input

If you do nothing about it, here is what happens:
Enter the loop, the height of the first mountain is put into mountainH variable. Nothing it’s done.
Enter the loop a second time, the height of the second mountain is put into mountainH variable (it erases the height of the first mountain because mountainH is a variable and, as such, contains only one value).
(…)
Enter the loop for the last time, the height of the last mountain is put into mountainH variable.

Then you are out of the loop, mountainH is out of your scope (at least depending on the language) and you cannot access it.

The point is: you need to do something in the for loop after reading mountainH. For example you can put it in an array that you will read once you are out of the for loop.

If that is the case, shouldn’t it just be a matter of saying “if the height of the mountain is equal to the ship’s height minus one, then fire”? What do you need the ship’s X axis if you’re given the height of the mountain each turn?

You are not given “the height of the mountain” but “the height of all mountains”.

If you think firing when the height of the mountain you are on is the ship’s height minus one, then just do it. And see by yourself what happens. :wink:

That’s what I’ve been trying, and failing. Then the mountains’ height is a list or what? That’s my main problem, how to access what number goes on which mountain and, therefore, when the ship has to fire.

I’ll try to explain what I already said in another way.

Tell me if I’m wrong, but you are trying to solve this problem in Python 3.
The default code is:

import sys
import math

# Auto-generated code below aims at helping you parse
# the standard input according to the problem statement.


# game loop
while 1:
    space_x, space_y = [int(i) for i in input().split()]
    for i in range(8):
        mountain_h = int(input())  # represents the height of one mountain, from 9 to 0. Mountain heights are provided from left to right.

    # Write an action using print
    # To debug: print("Debug messages...", file=sys.stderr)

    # either:  FIRE (ship is firing its phase cannons) or HOLD (ship is not firing).
    print("HOLD")

The line mountain_h = int(input()) reads one (and only one) height of a mountain.
The input function is reading the height of a mountain as a string. Here is some documentation on this function: https://docs.python.org/3/library/functions.html#input

Then the int function parse the string into a number. Here is some documentation on this function:
https://docs.python.org/3/library/functions.html#int

What we give you every round is the height of all mountains. You actually read them all because you are in a for loop. If you do not know what a for loop is, I think you should find a website that let you learn python 3, and then come back to train here.
Still, here is the documentation about the for loop:
https://docs.python.org/3/reference/compound_stmts.html#for

So the key point is the two following lines:

for i in range(8):
            mountain_h = int(input()) 

You enter the for loop, the value of i is 0, you read the height of the mountain on the left.
Then there is no more instruction in the loop so i is incremented to 1. 1 is still in range(8) so you read the height of the second mountain from the left.
Then there is no more instruction in the loop so i is incremented to 2. 2 is still in range(8) so you read the height of the third mountain from the left.
Then there is no more instruction in the loop so i is incremented to 3. 3 is still in range(8) so you read the height of the fourth mountain from the left.
Then there is no more instruction in the loop so i is incremented to 4. 4 is still in range(8) so you read the height of the fifth mountain from the left.
Then there is no more instruction in the loop so i is incremented to 5. 5 is still in range(8) so you read the height of the sixth mountain from the left.
Then there is no more instruction in the loop so i is incremented to 6. 6 is still in range(8) so you read the height of the seventh mountain from the left.
Then there is no more instruction in the loop so i is incremented to 7. 7 is still in range(8) so you read the height of the eighth mountain from the left.
Then there is no more instruction in the loop so i is incremented to 8. 8 is NOT in range(8) so you step out of the for loop. The next instruction in the default code above is: print("HOLD").

So at any round, you read the height of all mountains. It’s up to you to do what you want with these information.

*i is not really incremented but goes to the next value of the for loop. range(8) creates the sequence [0, 1, 2, 3, 4, 5, 6, 7] so i will take each of these value one after another. In this case, it is the same as incrementing i.