Javascript. I’m very new to coding. I can get Thor to move in all of the directions, but when he moves NE, NW, SE, SW he doesn’t make it to the light, he is not on the correct angle! Please help!
Hello Everyone!
I resolved this puzzle in C++, and now I’m trying to solve it using python.
I checked the available solution and my code. More or less they match, but , still, I fail the last two tes cases.
Here is reported the screenshot of the error. The light is set in (0,17) and Thor starts from(31,5). My output is WS, nut the expected one is NE.Someone could tell me why Thor should go North, if the light is set in the South?:
Thanks in advance
Do you update by yourself Thor’s coordinates?
No.
I mean, I play the test case and stop.
Sorry, maybe I don’t understand the question…
You have two choices:
- Either you print ALL the moves in one string. You don’t need to update Thor’s coordinates by yourself.
- Or you print the moves step by step. You need to update Thor’s coordinates by yourself.
does anyone know what another solution for this puzzle is maybe using code like this I am using C#
class player
{
static void Main(string[] args)
{
string[] inputs = Console.ReadLine().Split(’ ');
int lightX = int.Parse(inputs[3]);
int lightY = int.Parse(inputs[8]);
int initialTX = int.Parse(inputs[3]);
int initialTY = int.Parse(inputs[6]);
}
I started Codingame 2 Days ago and have no previous Codin-Experience except “The Morpheus Tutorials” from Youtube (Python/Bash) and a OpenHPI-Course for Java (all German, so sry for bad english^^).
I have a Problem with the number of possible turns for Thor to walk.
My Code worked for Test 1 and 2, but in the 3rd it failed, because Thor had only 15 turns to walk, what wasn’t enough to reach the target. So i looked for the hints and changed the code to the one, which stands also in the solution (I didn’t see differences, expect the comments where on different lines). But again just 15 turns to go.
Then I copied the solution and tested it.
This time, Thor had 32 turns to go. Has somebody an Idea, what wen’t wrong with my solution?
The Solution and also my code didn’t use the given codeline:
[…]
int remainingTurns = in.nextInt();
[…]
@Eldan
I had a quiet similar Problem (but maybe not the same).
I did in Java, and my Output was the x+y-coordinate. Out of my code, it results in “WS”.
So i changed the output to y+x-coordinate. The new result was SW, which was the expected output.
Maybe you have a similar Problem.
help im doing what the hints are giving me but does not work
Optimal angle
Hey there, sorry if this sounds bad. I’m stuck at the task of the Optimal angle. I don’t even understand what exactly it means
Since Thor’s energy fails, I’m trying to set up the code for him to go through the path of less energy. How can I access the energy values?
Thanks!
“SW” is a valid output, whereas “WS” is an invalid output.
HI the snippet does not work for the forth case.
please help me whats wrong. i have used java
while (true) {
String dir = "";
int remainingTurns = in.nextInt();
if(tx>lightX) { tx-=1; dir="W"; }
else if(ty>lightY){ ty-=1; dir="N";}
else if(tx<lightX){ tx+=1; dir="E";}
else if(ty<lightY){ ty+=1; dir="S";}
else if( (tx>lightX) && (ty>lightY)){ tx= tx- 1;ty= ty-1; dir="NW";}
else if( (tx<lightX) && (ty<lightY)){ tx= tx+1; ty = ty+1; dir="SE";}
else if( ( tx>lightX) && (ty<lightY)) { tx= tx-1;ty=ty+1; dir="SW";}
else if( (tx<lightX) && (ty>lightY)) { tx = tx+1;ty =ty-1; dir="NE";}
System.out.println(dir);
}
Guys I have taken an approach with the diference of the x-coordinates and the difference of the y-coordinates. Took this approach because I wanted to try something more complex than the suggested solution. I seem to have a mistake somewhere, although I am pretty sure I have none. Can someone help please.
Here is the code:
// game loop
while (1) {
int remainingTurns; // The remaining amount of turns Thor can move. Do not remove this line.
cin >> remainingTurns; cin.ignore();
int difx = lightX-initialTX;
int dify = lightY-initialTY;
float mathquot=dify/difx;
string out ="";
if(difx==0 || dify==0){
if(difx=0){
if(dify<0) out="W"; initialTY-=1;
if(dify>0) out="E"; initialTY+=1;
}
if(dify=0){
if(difx<0) out="N"; initialTX-=1;
if(difx>0) out="S"; initialTX+=1;
}
}else{
if(mathquot>0 && (difx>0 && dify>0)) out="SE"; initialTY +=1; initialTX+=1;
if(mathquot>0 && (difx<0 && dify<0)) out="NW"; initialTY -=1; initialTX-=1;
if(mathquot<0 && difx<0) out="SW"; initialTY +=1; initialTX-=1;
if(mathquot<0 && dify<0) out="NE"; initialTY -=1; initialTX+=1;
}
// Write an action using cout. DON'T FORGET THE "<< endl"
// To debug: cerr << "Debug messages..." << endl;
// A single line providing the move to be made: N NE E SE S SW W or NW
cout << out << endl;
}
}
Please don’t post your code in the forum.
Several errors, you have syntax errors in your conditions a single = and two can make a huge difference. You have also an inversion in outputs NS WE when difx ==0 or dify ==0.
Also take the time to debug your code by yourself, by printing into the stderr in order to see how your program behave.
@j91 there is a logic flaw in your code, as soon as tx>lightX the rest of your test is not explored…
Hey,
If you’re having trouble with the final answer, it’s because you can’t use a single direction. You have to use an angle and then a single direction so you don’t run out of energy. At first I thought it was an error but later realized that it’s not. Btw I used if statements and loops within it, hope that helps.
My Logic for NE is :
if( initialTX > lightX && initialTY < lightY){
initialTX–;
initialTY++;
print(‘NE’);
}
When i run 3rd test case this logic does not seem to be working properly. Results are -
Thor’s ready to go.
Thor position = (31,4). Light position = (0,17). Energy = 44
Thor’s moving…
Thor position = (32,3). Light position = (0,17). Energy = 43
(instead of Thor position = (30,5). Light position = (0,17). Energy = 43 )
Thor’s moving…
Thor position = (33,2). Light position = (0,17). Energy = 42
(instead of Thor position = (29,6). Light position = (0,17). Energy = 43 )
…and ultimately thor goes out of the grid.
I guess there is some kind of problem. Did anybody faced this type of thing?
NB: 4th test case works fine.
Move your thor diagonally first .Otherwise he’ll be getting out of energy
Hi, Can you please advise whí i gwt the error message:
error: bad operand types for binary operator ‘&&’
} else if ( thorX = lightX && thorY < lightY) {
^
i accepts the few first line with the same than suddenly error.
pls see code below:
ue) {
int remainingTurns = in.nextInt();
if ( thorX < lightX && thorY < lightY) {
String directionT = "SE";
} else if ( thorX > lightX && thorY > lightY) {
String directionT = "NW";
thorX = thorX-1;
thorY = thorY-1;
} else if ( thorX > lightX && thorY < lightY) {
String directionT = "SW";
thorX = thorX-1;
thorY = thorY+1;
} else if ( thorX < lightX && thorY > lightY) {
String directionT = "NE";
thorX = thorX+1;
thorY = thorY-1;
} else if ( thorX = lightX && thorY < lightY) {
String directionT = "N";
thorY = thorY+1;
} else if ( thorX = lightX && thorY > lightY) {
String directionT = "S";
thorY = thorY-1;
} else if ( thorX < lightX && thorY = lightY) {
String directionT = "w";
thorX = thorX-1;
} else if ( thorX > lightX && thorY = lightY) {
String directionT = "E";
thorX = thorX+1;
}
Assuming that’s java, try == instead of = .
You’re also going to run into trouble with your directionT variable; since you’re declaring it inside the conditional blocks, it is not going to exist later on when you try to use it.
Thank you it worked. the == was the issue.
Can you pls advise why i would not accept the thw angle test cases?
while (true) {
int remainingTurns = in.nextInt();
if ( thorX < lightX && thorY < lightY) {
directionT = "SE";
} else if ( thorX > lightX && thorY > lightY) {
directionT = "NW";
thorX = thorX-1;
thorY = thorY-1;
} else if ( thorX > lightX && thorY < lightY) {
directionT = "SW";
thorX = thorX-1;
thorY = thorY+1;
} else if ( thorX < lightX && thorY > lightY) {
directionT = "NE";
thorX = thorX+1;
thorY = thorY-1;
} else if ( thorX == lightX && thorY < lightY) {
directionT = "S";
thorY = thorY+1;
} else if ( thorX == lightX && thorY > lightY) {
directionT = "N";
thorY = thorY-1;
} else if( thorX < lightX && thorY == lightY) {
directionT = "E";
thorX = thorX-1;
}
// The remaining amount of turns Thor can move. Do not remove this line.
// Write an action using System.out.println()
// To debug: System.err.println("Debug messages...");
// A single line providing the move to be made: N NE E SE S SW W or NW
System.out.println(directionT);
}