# Power Of Thor - Episode 1 - Puzzle discussion

#483

I going through almost 2 yrs of comments and I am in bandwagon of 50%ters but when I run my code in my own environment with same energy count, I get correct results my thorY stops at 17 and then thorX keeps decrementing till I reach 0. so I think my code works but fails here. I have written in it Go

#484

I have a strange problem, the recommended solution is that we first check if Thorâs X position is greater than the lightâs, if yes we move him West and update his position else if Thorâs X position is less than the lights, we move him East and update his position. Same goes with the Y, and at the end we print the direction we got.

My simple question is why this algorithm does not work if we check first if Thorâs X position is less than the lightâs and after that if itâs more? Same for the Y. The algorithm should be symmetrical and there should not be any problem if you check if Thorâs position is less than the lightâs.

Does someone have any insight on this because I have been thinking on this all day and cannot figure out why this happens.

#485

It is âsymmetricalâ. I guess there is a bug in your program.
If you need some help, Iâll advise you to define this: [quote=âbenchpressarchon, post:484, topic:30â]
this algorithm does not work
[/quote]

#486

When I say the algorithm does not work I mean that Thor just wanders off the light from the start. Also the only case that works is the first one.

You can test it for yourself really easily (since I am not allowed to paste code here). Check if Thorâs X is less than lightâs Y (same with Y) and you will see it does not work.

#487

Hi there!
I am newbie in programming,but willing to get nuts.(Java lang.)
Can some one explain me where am i wrong?

// game loop
int thorX= initialTX;
int thorY= initialTY;
while (true) {
int remainingTurns = in.nextInt();

``````        int directionX= lightX;
int directionY= lightY;

if (thorX>lightX)
{directionX=E;
thorX=thorX-1;}
else if(thorX<lightX)
{directionX=W;
thorX=thor+1;}
else if (thorY>lightY)
{directionY=S;
thorY=thorY-1;}
else if(thorY<lightY)
{directionY=N;
thorY=thorY+1;
}
System.out.println(directionX+directionY);
``````

TY

#488

Hello bumbl3bee
Could you include the error messages you get when you press play? (compilation errors appear in the Console)

My feedback:
`directionX` & `directionY` should be declared as `String` not `int`
if `thorX` is greater than `lightX` you should be going west (`directionX=W`)
if `thorY` is greater than `lightY` you should be going north (`directionY=N`)
You should output `directionY + directionX` rather than `directionX + directionY`. âSWâ is valid but âWSâ is not.

If you are having trouble with this puzzle, Iâd like to direct your attention to the âHINTSâ button on the left menu (when inside the IDE).

#489

Can we get right into the news?

#491

Program did not provide input in time? Planet was destroyed?

#492

I am having trouble with the last two test cases, any insight? I realize that I may have to deal with diagonal movements.

#493

I think the test cases need to be beefed up overall because there is no North and East/West so you can create code that passes the test cases without being optimal for all real cases.

#494

Same in PHP, I can see in the video that Thor go on the light but it doesnât pass the test anywayâŚ

#495

Same issue in Python3. Fix that I want my 100%!

#496

I Have the same problem, I donât understand what you mean by that. Could you explain how I can change the direction from an angle to straight line to prevent out off bounds. Thank you.

#497

Nvm my solution worked out. Thank you

#498

In C# my code does not pass the third and fourth test casesâŚthe code seems right:

``````if(thorY > lightY)
{
dirY = "N";
thorY--;
} else if(thorY < lightY)
{
dirY = "S";
thorY++;
}

if(thorX > lightX)
{
dirX = "W";
thorX--;
}else if(thorX < lightX)
{
dirX = "E";
thorX++;
}
Console.WriteLine(dirY + dirX);
``````

So im stuck in this last 2 casesâŚcan you help me?

#499

``````string dirX = "";
string dirY = "";

// game loop
while (true)
{
...
}
``````

This is not your code :

``````// game loop
while (true)
{
string dirX = "";
string dirY = "";
...
}
``````

A long time ago, programming languages were requiring the user to declare all its variables at the top of its functions. It makes things easier for the guy who writes the compiler, not so much for the user. It always better to declare a variable in the nearest range where you need it. Better yet, avoid assigning it some kind of default value gratiously instead of letting any modern compiler detect any hole in your code. Of course, the very best solution is to use a functional language which has no mutable variable at all.

#500

Thank you for the feedback, although, i did declare the variables in the section of the code that i needed. This is my full code:

``````while (true)
{
int thorX = initialTX, thorY = initialTY;
int remainingTurns = int.Parse(Console.ReadLine()); // The remaining amount of turns Thor can move. Do not remove this line.
string dirX = "", dirY = "";

if(thorX > lightX)
{
dirX = "W";
thorX--;
}else if(thorX < lightX)
{
dirX = "E";
thorX++;
}

if(thorY > lightY)
{
dirY = "N";
thorY--;
} else if(thorY < lightY)
{
dirY = "S";
thorY++;
}

// Write an action using Console.WriteLine()
// To debug: Console.Error.WriteLine("Debug messages...");

// A single line providing the move to be made: N NE E SE S SW W or NW
Console.WriteLine("{0}{1}", dirY, dirX);
}``````

#501

Each time through the loop, you are resetting your `thorX` and `thorY` to their initial values. What is the point of incrementing or decrementing the variables if you reset them to initial value each time through the loop?

• danBhentschel

#502

So unusual! Thatâs the first Thor I see which does update its position but also reset it along the way. My guess was indeed wrong and you need to move the thorX/Y variables out of you loop instead of moving dirX/Y inside it.

#503

why did walking ???