Power Of Thor - Episode 1 - Puzzle discussion

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

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.

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]
:wink:

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.

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

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).

Can we get right into the news?

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

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

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.

Same in PHP, I can see in the video that Thor go on the light but it doesn’t pass the test anyway…

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

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.

Nvm my solution worked out. Thank you

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?

This is your code:

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.

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);
        }

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? :slight_smile:

  • danBhentschel

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.

why did walking ???