Power Of Thor - Episode 1 - Puzzle discussion

can you share to me the code which doesn’t pass test 3?

Is there a reason this logic isn’t working in C++? The game keeps allowing Thor to go past the limit I set in my if statement, which I have set up as follows:

if (thorY > lightY && thorY > 0)
//code
else if (thorY < lightY && thorY < 17)
//code
else
//code
if (thorX > lightX && thorX > 0)
//code
else if (thorX < lightX && thorX < 39)
//code
else
//code

the compound test should ensure that Thor never reaches thorY of 18 OR thorX of 40, but case 3 keeps failing with a Y position of 18. Am I doing something wrong here?

Assuming that the code you cut out in your example is updating thorX and thorY, this looks reasonable, if redundant. I would check for bugs in the part where you’re updating your position.

Also, what is the last else statement used for?

I update the position in a manner like this:
{
dX = “N”;
thorX–;
}
which seems fine, I don’t see the problem with it. The last test (&& thorX > 0) makes sure I don’t reach the edge of the map and fail. As for the last else, it sets the direction to blank, so the new direction will be only N, S, E, or W:
dX = “”;

I wonder why my code doesn’t pass test 3.
It still execute last else when lightY == initialTY

String direction = “”;
if(lightX == initialTX && lightY > initialTY)
{
direction = “S”;
}
else if(lightX == initialTX && lightY < initialTY)
{
direction = “N”;
}
else if(lightX > initialTX && lightY == initialTY)
{
direction = “E”;
}
else if(lightX < initialTX && lightY == initialTY)
{
direction = “W”;
}
else if(lightX > initialTX && lightY < initialTY)
{
direction = “NE”;
}
else if(lightX > initialTX && lightY > initialTY)
{
direction = “SE”;
}
else if(lightX < initialTX && lightY < initialTY)
{
direction = “NW”;
}
else if(lightX < initialTX && lightY > initialTY)
{
direction = “SW”;
}

1 Like

I advise you to read this thread a bit more. The answer to your question has been written many times :wink:

yes,you are right!

I solved . But I don’t understand why I need to update Thor’s position. I think the program provide updated position in every new turn already because I pass test 1 and 2 without updating Thor position, and Thor still can move!!

I can’t see how the last else will ever be triggered… that being the case, you never clear dY and so you send the previous turn’s value and run off the map.

The last else should have been triggered if (for example, on the X-axis) Thor’s X is 1 and the light’s X is also 1, so it would fail the first 2 tests and set the dX to “”. I set the same thing up in the Y as well (so if thorY is 1 and the lightY is 1, dY = “”).

I’m not sure if there is a bug on test case 3 and test case 4. I am only given 15 turns to complete test case 3 even though I have 31 energy left.

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

BUG!!! Javascript

[Removed the source code and the puzzle is not bugged]

<?php

fscanf(STDIN, "%d %d %d %d",
    $lightX, // the X position of the light of power
    $lightY, // the Y position of the light of power
    $initialTX, // Thor's starting X position
    $initialTY // Thor's starting Y position
);

// game loop
while (TRUE)
{
    fscanf(STDIN, "%d",
        $remainingTurns // The remaining amount of turns Thor can move. Do not remove this line.
    );

if ($lightY > $initialTY) echo ("S");
   elseif ($lightY < $initialTY) echo ("N");


if ($lightX > $initialTX) echo ("E");
    elseif ($lightX < $initialTX) echo ("W");

echo("\n");
}
?>

Hello everybody.
Why does not pass code 3 and 4 Test cases?

echo or echo -n ?

this question to me?
I did not understand.

When I read man echo, I find this that may help you:
-n do not output the trailing newline

I read the man for echo
http://php.net/manual/en/function.echo.php
and I did not find what you are writing.
Do you have any suggestions how to fix my code?

Hrm sorry, I talked about Bash, you talked about PHP. :blush:

The answer to your problem exists in this thread in many locations. Did you read it?

1 Like

Thank you, I found the problem.