Hello
i am new here
and i am trying to solve this problem
using C#
that is my code
i tested it on Visual Studio Console
and working good
but here
it doesn’t work
what is wrong
thanks
using System;
using System.Linq;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;
/**
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
{
static void Main(string[] args)
{
// game loop
while(true)
{
Mountain x = new Mountain();
x.Get_Highest();
Console.ReadLine();
}
}
}
public class Mountain
{
List Height = new List();
public void Get_Highest()
{
for (int i = 0; i < 8; i++)
{
int mountainH = int.Parse(Console.ReadLine()); // represents the height of one mountain
Height.Add(mountainH);
}
while (Height.Count >0)
{
for (int i = 0; i < Height.Count; i++)
{
if (Height[i] == Height.Max())
{
Console.WriteLine(Height[i]);
Height.Remove(Height[i]);
}
}
}
Why is it telling me I have 0% progress even though I successfully beat all of the 6 prompts? I’m using python, not sure if that has anything to do with it… but when I would move on to the next challenge, I’d get a box with a (!) in it. Am I supposed to leave the previous commands in the code? Maybe with (#)s, so it doesn’t actually try to run that many lines of code before getting to the part that relates to the current challenge…
Thank you in advance for any and all replies!
Your code must be generic enougth to beat all the challenge without any code change. If you change something, you need to beat all the challenge again.
Okay, i am at the very beginning of coding , so please, explain me how i can make a different loop for every stage of the game without needing to delete the old one? I am trying to learn JavaScript.
The standard approach to a puzzle (at least for me) is: Analyze the input.
Thankfully, the IDE gives you a working stub for this.
for JS it is
while (true) {
for (let i = 0; i < 8; i++) {
const mountainH = parseInt(readline());
}
}
As you can see, all the basic input loops are already in place. Add a simple “printErr (mountainH);” to the inner loop and watch what comes out of it for different test cases.
The two loops in this example are
while (true)
which loops over every turn, and
for (let i = 0; i < 8; i++)
which loops over each of the eight mountains.
For this basic puzzle, you do not need another loop, if you don’t want to.
There is something I dont understand, If Im here for learning how to code, why am I seeing loops and things like that? why do I have to give a solution to play the game if I dont know exactly how it work?
I does not have sense
Found a way to break the simulation so that an otherwise correct solution fails. Coding in PHP, using fscanf(STDIN, "%d", $mountainH); outside the for loop to initialize the “highest mountain” variable (instead of just setting it to zero) means that the ship never successfully shoots any mountain, even if it targets the correct mountain.
While using fscanf() takes longer than just setting a variable to zero, using “true” data and variable values whenever possible is good coding practice. It means code will fail earlier (“Fail Early, Fail Loudly”) and it means having as little junk data in an app as possible at any given time, which can be important if a piece of data is called upon at an unpredictable time (which should be avoided on its own, but still).
Maybe an addendum to the game rules saying that fscanf(STDIN, "%d", $mountainH); can only be used once, since it takes time to run and you could crash before two scans are completed, or something. Or say it can only be used as part of a scanning pass, not on its own, because of the way the scanner works or something.
When you debug this in Visual Studio, it outputs all 8 of those values every time, as the code reads.
When you play the solution above in the game, it does a very strange behavior where it sequentially reads the next console.writeline statement, one for each iteration of the loop.
The game should either:
Read my entire block, and fire 8 shots immediately, and destroy all 8 mountains (heh!)
Read the first line only, and shoot only mountain 0 during every loop.
But since the code in the game does not behave the way I expect it to, I don’t feel like I can trust the codingame compiler. Why did it step through my big block writeline answers one at a time? I didn’t do that! I sent the same 8 lines over and over again!
There’s no behavior difference between CG and Visual Studio; in both cases your program is writing 8 lines to Console.Out and then waiting for 8 lines to come back through Console.In. The difference is that Console.Out in VS goes to your screen while in CG it’s going to another program’s Console.In to wait for processing.
Console.In is a queue; first-in, first-out. No messages are removed from the queue unless something reads them. Since the puzzle specified that you enter one line per turn, that’s all it’s reading. You’re putting in 8 lines each time and the puzzle is only reading one, so the referee is still reading commands from turn 1 when turn 2 starts. That’s why it’s executing your commands sequentially.
I don’t agree with this. The code we write must be generic enough to pass all the tests. Otherwise, a gamer can “selectively” change the code to pass different tests.
So, I’m having some trouble understanding this. I’m extremely new to coding. Each time i finish one and move to the next one, when I change some of the code that solved the last one, a exclamation point pops up. I’m not sure how to solve all of them. I just need a nudge in the right direction.
Your program is supposed to solve all of the test cases… that means one program that will pass them all. When you change the program, it resets the result you got on the other test cases because it isn’t the same program anymore.
I honestly have no idea what to add or what to write out to do that. I know that one code is supposed to solve all of them, but I don’t understand in the slightest where to start or what to write or add. I don’t understand the hints at all either.
The platform doesn’t validade my answer, but it’s right
while True:
for i in range(8):
mountain_h = int(input()) # represents the height of one mountain.
if i == 0:
target = i
highest_mountain = mountain_h
else:
if mountain_h >= highest_mountain:
target = i
highest_moountain = mountain_h
print(target)
If its the inputs that are trouble, then make sure that this piece of code is functional, it should give you a version in your own code when you had started the puzzle:
for i=0,8-1 do
H[i] = tonumber(io.read()) – represents the height of one mountain.
end
(thats lua btw)