Power Of Thor - Episode 1 - Puzzle discussion

Help please, I am stuck here. I tried the 8-if-conditions solution, but for some reason, the code below only works for cases 1 & 2 but not 3 & 4. I don’t know what went wrong!

    char direction;
    if (thorX < lightX && thorY < lightY) {
        direction = 'SE';
        thorX++; thorY++;
    }
    if (thorX < lightX && thorY > lightY) {
        direction = 'NE';
        thorX++; thorY--;
    }
    if (thorX > lightX && thorY < lightY) {
        direction = 'SW';
        thorX--; thorY++;
    }
    if (thorX > lightX && thorY > lightY) {
        direction = 'NW';
        thorX--; thorY--;
    }
    if (thorX == lightX && thorY < lightY) {
        direction = 'S';
        thorY++;
    }
    if (thorX == lightX && thorY > lightY) {
        direction = 'N';
        thorY--;
    }
    if (thorX < lightX && thorY == lightY) {
        direction = 'E';
        thorX++;
    }
    if (thorX > lightX && thorY == lightY) {
        direction = 'W';
        thorX--;
    }

    printf("%c\n", direction);

try printing a debug message like “I chose this direction: XX” in each of your 8 ifs. You’ll quickly see where the issue lies.

I don’t think the issue is fixed, it’s stuck calculating my score too

Oh nevermind, it appears to have resolved itself after about 15 minutes

I think there is a bug. I’m running out of my 14 turns. Even with a simple diagonal angle to the light, and simply hardcoded go SW I can’t read the target within 14 turns.

Looks like the target is too far to ever reach.

this puzzle was one of the oldest of CG.
it is solved by 237882 players.

there is no bug in the puzzle.
there is no test/validator limited to 14 turns to reach target.

1 Like

Looks fine to me

I also had the same reaction. I started this game with Java to restart to be a developper :slight_smile: and my first test was not good, because “time out” = need to be done in 14 turns.
I just tell that because dwarfie seems very confident, but, maybe something to check.
I’ll retry it today :wink:

I found 5 minutes to finish it. Yes, there is no limits on the number of turns. Maybe, the 14 we found was more because we did the same bug and so to avoid to have an infinite loop we had a time out? So maybe, just the “exception” is not clear :wink:

If you hardcode SW, you get this error:

Failure: Thor wandered off the path and died (invalid position).

Try updating Thor’s coordinates.

1 Like

else if (thorY < lightY && thorY < 17) {
directionY = “S”;
thorY++;
I am failing the 3rd test because thorX and thorY are out of scope. I’m struggling to figure out how to update thors position inside the while loop, any tips?

I’m a little confused -
For the 3rd and 4th tests, why is Thor suddenly walking in the opposite direction. According to the code, he should be walking towards the energy source, but he’s walking away from it.

I’m having the same problem on submission.
Edit : Nevermind, I found the problem. I miswrote the code. But now apparently I have to update Thor’s current position for the diagonal tests, and I have no idea how to do that.

Hello,
Why in test 4 exemple has 36 steps and in javascript developmet has 14?

   i realy dont get why this wont work...?

"error: variable directionX might not have been initialized

        System.out.println(directionX+directionY);
                           ^

while (true) {

       int remainingTurns = in.nextInt();// The remaining amount of turns Thor can move. Do not remove this line.
       String direction;
       
       if(dify==0){
           if(initialTx<lightX){
                directionX="";
                directionY="E";
                initialTx++;}
             else{
                 directionX="";
                 directionY="W";
                 initialTx--;
                 }
               }
       else if(difx==0){
           if(initialTy>lightY){
                directionX="N";
                directionY="";
                initialTy++;}
             else{
                 directionX="S";
                 directionY="";
                 initialTy--;
             }
       }
       else if(difx!=0&&dify!=0){
           if(initialTx<lightX&&initialTy<lightY){
           directionX="S";
           directionY="E";
           initialTx++;
           initialTy++;
           }
           else if(initialTx<lightX&&initialTy>lightY){
               directionX="N";
               directionY="E";
               initialTx++; 
               initialTy--;}
            else if(initialTx>lightX&&initialTy<lightY){
                directionX="S";
                directionY="W";
                initialTx--;
                initialTy++;}
            else{
                directionX="N";
                directionY="W";
                initialTx++;
                initialTy++;}
                }
            else{
                remainingTurns++;
                }

        // Write an action using System.out.println()
        // A single line providing the move to be made: N NE E SE S SW W or NW
        System.out.println(directionX+directionY);
    }
}

}

Because the Java compiler cannot prove the variable has a value due to the if-statements. Explicitly defining it as null or an empty string should work.

1 Like

This is not evident from the given instructions (that we have to first print the move and then update the coordinates).
Why would it be wrong if I first update the coordinates and then print a move?
I should be able to solve this question without knowing the details of how the solution checker is working to check my solution.

I don’t know which post you’re replying to (I went back a couple of months but couldn’t find it), but if I gave you the impression that it matters when you update your internal variables, I apologize.

All the checker cares about is what you output to it. You can update the variables any way you choose and at any time. Some people don’t even bother with position tracking and use loops instead.

edit: Found it. The problem with the code I was commenting on was that he was updating his coordinates before he even decided which direction was the correct one to go. So his directions were always one turn ahead of where he actually was.

Swift solution_

[Erm, no]

You can click on the Reply Icon and see what they answered to. (On the post itself, which pops up, you can also click on the little arrow to “scroll up” to that exact post)
It’s a post “First, move. THEN update coordinates.” from you where you answered to someone who updated their coordinates first, then checked in which direction they have to move.
No, it doesn’t matter in which order you do what, as long as you reach the goal in the given moves.

@saurabhrk:
The solution checker checks only whether Thor is on a given coordinate when his allowed movecount has reached 0 or he is on the given coordinate earlier.

1 Like

I didn’t get it.
Isn’t this peace of code inside main WHILE updates Thor’s coordinates?

initial_tx = int(input())
if initial_tx < light_x:
initial_tx = initial_tx + 1
print(‘E’)