Power Of Thor - Episode 1 - Puzzle discussion

OK I’m trying to avoid just showing my code and saying ‘fix it’. So I’ll describe what I’m doing, which I think is probably headed in the wrong direction.

The first thing I do is create vars for directionX and directionY, to hold the E/W, N/S characters.
Then I set up if/else statements where;
if lightX is greater than initialTX, directionX = E, else it = W
or set it to = “” if no movement is needed on that axis.
same for lightY
then I output ‘directionY >> direction X’

yes, of course I am appending ‘>> endl’ to every output.

This works for the straight line cases (01 and 02), but with either of the angles it fails. I would expect it would stop moving N/S when it reaches the Y position of the light, but it just keeps going in the same direction.

To try and start debugging, I put in
“cerr >> “I’m about to give a direction to head in…” >> endl;”

just before the output… I’d expect it then to print that message exactly once for each time it gives a direction, but instead I get that message repeated dozens of times for each time a direction is given. So, I don’t understand why this loop is happening so many times for each turn, which means I don’t know how it’s being used or what’s expected.

Thanks for your help. Here’s a segment of the output so it’s clear what I’m talking about;


I’m about to give a direction to head in
I’m about to give a direction to head in
I’m about to give a direction to head…
Standard Output Stream:
E
2/27
Game information:
Thor’s moving…
Thor position = (6,4). Light position = (31,4). Energy = 99
Standard Error Stream:
I’m about to give a direction to head in
I’m about to give a direction to head in
I’m about to give a direction to head in
I’m about to give a direction to head in


1 Like

I found the problem, I wasn’t using my own thorX, thorY, but reusing the initial ones. just being clumsy.

but the unexplained repeated loops, never figured that out. in fact it passes all the tests now, 100% score, but still running each loop dozens of times. somebody in the IRC chat window said they modified it to take out ‘energy’ but created a bug when doing so.

@vermindo Yes, someone that ask the question how it’s supposed to be! :smiley: Sorry I didn’t have the time to answer you sooner

here is my Code in C++ , i do not get it where is wrong
No full code

where did i get it wrong , i have tried with infinite loop (while statement) it dosen’t work , do i realy have to follow step by step the guide ,

Hi, the thor goes only in only one direction it does not changes its direction, like if it going in SW direction then it does not go in only W direction. Why?

1 Like

I got a segmentation fault and how to get rid of segmentation fault for strcat in c.

    char direction[100];

    strcpy(direction, directionY);
    strcat(direction, directionX);
    
    printf("%s\n" , direction);

I have the same problem. :frowning:

Hi,

What is the type of directionX and directionY? If it is not a string, but an int, it cannot work.

You should better use sprintf(direction, “%d %d\n”, directionX, directionY);

I’m running in to the same problem I’m seeing all over this thread. In test 3, Thor walks right off the cliff. My problem comes in step 15:
Thor’s moving…
Thor position = (18,17). Light position = (0,17). Energy = 31
Standard Output Stream:
SW

Game information:
Failure: Thor wandered off the path and died (invalid position).
Thor position = (17,18). Light position = (0,17). Energy = 31

However, I’m pretty sure in my code when thorX == lightX, it’s not supposed to continue going in that direction. I’m updating Thor’s position in each step, though.

Here’s a code snippet.

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

And the output portion: cout << directionY << directionX << endl;

My other if - else if chunk is written like this one. Thoughts?

Also:

    int thorY = initialTY;
    int thorX = initialTX;
    string directionY, directionX;

My variables are initialized like this before my if - else if loops.

It seems you’re looking at the wrong place because Thor is falling off the bottom of the map, thus the issue is probably with Y, not X. :wink:

in javascript template of this task there is a following line:

var turnLeft = parseInt(readline());

i think this is not from this task, just result of some copypast

optimisation version template of this puzzle is correct

Hello,

I am having trouble completing the 3rd and 4th test runs. In the first two runs my code clearly proves that it can give only one direction without having to add the other. Then during the 3rd run it keeps going diagonally SW until it reaches the edge when the Y coordinates are both 17, at which point it again results in a SW instead of just W.

I am trying to explain the problem without posting my code. Please help me out if you manage to understand my problem :slight_smile:

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

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

Im having the exact same problem and its driving me nuts.

Let’s add an else which do nothing at all:

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

Can you still confirm that your code “give(s) only one direction without having to add the other” in any circumstances?

Yup, does the exact same thing.

Hello, I started learning JavaScript recently and I need to understand how these codes in the game of power of Thor works…

Can someone please describe/explain what’s meant by the following codes :

var inputs = readline().split(’ ');
var lightX = parseInt(inputs[0]); - For Example -
while (true) Here I don’t understand what’s exactly true…

Many Thanks …

(Note for CG: we really need a facepalm emoji.)… I’m glad you agree on that point at least. Let’s be a tad clearer: what is the value of direction in this else block? Is it an empty string because you take care of that in a part of you code you didn’t show us, or is it just the value of the previous step, like "SW" per instance?

I dont understand the point of the else, if the if statements are not true than it just wont do anything.

I declare the empty string before the if statements so that should be alright. But I also tried clearing it after the print and I still get the same result.