The Descent - Puzzle discussion

does any one have solution for python 3? im really struggle with it.

Yes, I have a solution.
Whatā€™s your problem?

Iā€™m having quite a bit of trouble in python 3, I canā€™t say if itā€™s a bug or working as intended but even the ā€œsolutionā€ from the hints wonā€™t work. Here is my code:

hmax = 9
imax = 0
while True:
for i in range(8):
mountain_h = int(input())# represents the height of one mountain.
if mountain_h >= hmax:
imax = i
pass
pass
hmax = hmax -1
print (imax)

It passes every test but ā€œstrong mountains 2ā€, just as the ā€œsolutionā€. Iā€™ve noticed it is because at one point in the test the same value is given by 2 different indices and the code ignores both indices and prints the next index with greatest value. Iā€™ve been studying code for a week now and I would highly appreciate if you guys could help me.

hi a question.
what does the line cin>> mountainH represents.
I mean it get the height of a mountain sure, but which one. That i don;t understand. To me it should get the height of a each mountain at the time, precisely for each new i.

also can somebody explain to me why we have the cin,ignore();
Thx

HAHAHA was about to post this exact thing for C++. Trying to do the mountainH == 9 optimization I was scratching my head and came to the same conclusion. Anyway, Confirmed that it requires the scanf (cin read for C++) all 8 times to work properly.

hello iā€™m new student
plz anyone help me ?

My Javascript code. On descending, it skips mountain 4(shoots at 0) but correctly shoots mountain 5 after. Anyone have any idea what is going on there? FYI, the ā€˜forā€™ section is indented in the code, itā€™s just not showing up here for some reason.

while (true) {
for (var i = 0; i < 8; i++) {
var target = 0;

   if (parseInt(readline(i)) > parseInt(readline(target))){
       target = i;
    }
    print(target);
}

}

So shoots 0,1,2,3,0,5 then crashes because it didnā€™t shoot 4ā€¦

Game info at start and end respectively:

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

End:

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

you set target=0 inside the loop
you print the highest mountain inside the loop (without even reading all the input)
you read two lines in your comparison
readline has no arguments

just after a short look, not guaranteed to be a complete list.

I donā€™t understand most of your reply :confused:

If it runs through the list each time, why does establishing the target variable inside the loop matter?

And what do you mean when you say it doesnā€™t read all of the input? Shouldnā€™t the line

parseInt(readline(i))

check the height of each mountain against the current highest

parseInt(readline(target))

when the for loop runs?

while (true) {
	for (var i = 0; i < 8; i++) {
		var target = 0;
		//...
		print(target);
	}
}

is wrong, as you output something before even knowing the complete input. How shall you find the highest mountain without knowing all mountains? Try this instead:

while (true) {
	var target = 0;
	for (var i = 0; i < 8; i++) {
		//...
	}
	print(target);
}

also it is not parseInt(readline(i)) but parseInt(readline())

if (parseInt(readline()) > parseInt(readline()))
reads two lines at once. You compare line 1 with line 2, then line 3 with line 4. But not line 2 with line 3, 4 with 5 and so on.

When I put the print outside the for loop (which is more logical) I get a Timeout error.

And I think Iā€™m really confused on how readline() operates and what line Iā€™m reading for that matter. I canā€™t find an explanation with examples in a language I am familiar with. How does it know which line to read? Does it read all lines? If it reads all lines, wonā€™t parseInt() still only return the number for the first mountain?

Is the line being read an array with only the mountain heights? Or is it the list presented under Game Information?

1 Like

readline() does, what the name says: it reads one line.
For whatever reason I canā€™t send you a message in the chat. You can send me your code in private message via chat or forum if you want me to have a look at it.

Hi !
I have a critical question about the input system (Python3) :
Here is the hint I where provided with :

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

And here is my question : How can you expect the input() fonction to return different values according to the value of i ?

I must be missing something realy obvious, but what ?

You have to store the values in a list by yourself.

1 Like

Itā€™s not the value of i that changes the result of calling input(), itā€™s the fact that input() reads the next line from the input source each time it is called. Once a line is read, it is gone like it never existed (unless you save it in your code somewhere).

Hi, noob here. I tried what aLee2 said and itā€™s still not working. Probably a stupid mistake. Let me know what you think.

Edit: No full code

You have to reset max each turn (game loop) otherwise it will keep the first highest mountain.

Iā€™ve read some complains about the python3 version. Which I donā€™t know if it was just meā€¦ but the animation was broken. Anyway I got this solution, hope it helps someone.

Admin Edit no need to give solutions to others.

I wanted to say that I came here because some Hackerrank challenges are so badly explained that I didnā€™t want to waste more time trying to figure out what are they asking for, but I found this even more confusing.

First, the explanation of the problem says:

Firing on a mountain will only destroy part of it, reducing its height.

But when you try and shoot a mountain, it gets reduced to height 0. How is that destroying part of it?

Also, the fact that you donā€™t see the code that prints the heights makes it hard to understand whatā€™s happening. Why do I have access to the cin for loop but not the rest? Ok, I build a vector inside (Iā€™m using C++), but apparently there is another vector, array or whatever somewhere, storing the heights; it would make more sense if I already had access to that.

I donā€™t know, pretty disappointing.

Thanks for the feedback and welcome to Codingame.

Not all mountains exhibit that behavior. Some of them in the later test cases take multiple shots to reduce to 0. I guess the wording could be a little clearer (ā€œSome mountains take multiple shotsā€), but since the line right above it says the objective is to fire at the highest mountain, it shouldnā€™t affect your strategy.

Not sure what you mean about not having access to the heights; you read them from cin every round, run whatever code you need to decide on your answer, and then give the answer on cout. Then the game referee updates the heights and sends them back to cin for the next round. You donā€™t have access to the referee other than through cin and cout. cerr is also available for debugging; it shows on your screen but is not sent to the referee.

2 Likes