Power Of Thor - Episode 1 - Puzzle discussion

yes i could google it sry. the operator != could be a solution but I’m curious and I wanted to know why == isn’t working in this case

Because there are many possible combinations of dx and dy where you have to output SE / SW / NE / NW, and dx * dx == dy * dy is not a necessary condition for that.

(I edit the code I did a mistakes)

I wanted to check if dx=dy and authorize the diagonal only if dx=dy or dx=-dy

I think my problem is the “==” I don’t understand why the code don’t do anything when I use and "== "

Have you tried printing to the error stream what the values of dx and dy are before your code checks the condition?

printing to the error stream what do you mean? return the value of dx and dy to check if the are egal ?

Printing to the error stream helps you understand what the values of some variables are at certain points of your code. This can help you check whether the values calculated by the code are those you have expected, and also facilitate the debugging process.

On CodinGame, for C++, you print the answer to the standard stream by using cout. And to print other things to the error stream, you use cerr instead, e.g.

cerr << dx << " " << dy << endl;

Then you can check why your condition does not seem to work in the way you think it should.

thanks a. lot. 5DN1L I’m not a native English speaker and I didn’t understood well the function “cerr” my program working now. that was really helpful ! and I’ve done what. I wanted to do : use a rule for diagonals

if (dy == dx){
cout << move_y << move_x << endl;
} else if (dx > dy){
cout << move_x << endl;
if (move_y == “S”){
posY = posY -1;
}else if (move_y == “N”){
posY = posY+1;
}
}else if (dy > dx){
cout << move_y << endl;
if (move_x == “W”){
posX = posX-1;
}else if (move_x == “E”){
posX = posX+1;
}
}

1 Like

Thank you so much, I updated Thor coordinates and solved it.

Two things. First, it appears that you can go to negative index. Second, my code wasn’t working so I used the pseudocode hint and it was almost the same code but as I said, didn’t work. Idk, maybe is the pseudocode the one that needs changes? Idk

class Player

{

static void Main(string[] args)

{

    string[] inputs = Console.ReadLine().Split(' ');

    int lightX = int.Parse(inputs[0]); // the X position of the light of power

    int lightY = int.Parse(inputs[1]); // the Y position of the light of power

    int initialTx = int.Parse(inputs[2]); // Thor's starting X position

    int initialTy = int.Parse(inputs[3]); // Thor's starting Y position

    int currentTx = initialTx;

    int currentTy = initialTy;

    // game loop

    while (true)

    {

        int remainingTurns = int.Parse(Console.ReadLine()); // The remaining amount of turns Thor can move. Do not remove this line.

        string movement = "";

        // Write an action using Console.WriteLine()

        // To debug: Console.Error.WriteLine("Debug messages...");

       

        if(lightY > currentTy){

            currentTy -= 1;

            movement +="S";

        }

        if(lightY < currentTy){

            currentTy += 1;

            movement +="N";

        }

        if(lightX > currentTx){

            currentTx += 1;

            movement +="E";

        }

        if(lightX < currentTx){

            currentTy -= 1;

            movement +="W";

        }

       

        // A single line providing the move to be made: N NE E SE S SW W or NW

        Console.WriteLine(movement);

    }

}

}

The pseudo-code is fine. Your code is wrong:

  • One + and one - should be swapped.
  • The last but one line should be current Tx -= 1.

By the way, in the future, please state the programming language in your question, and also use the </> button in the formatting toolbar to format your code properly. Thanks!

Hi! I didn’t understand why when I submit the code scenario 03 Easy Angle does not pass. I draw the game board and simulate by hand using my coding calculations. I’m stuck. Would you mind to help me?
Here is my piece of code when Thor and the light was not in the same axis:

else if (initialTx > lightX) {
        if (initialTy < lightY) {
            let lightPosition = lightX + lightY;
            let thorPosition = initialTx + initialTy;
            while (thorPosition > lightPosition) {
                console.log('W')
                thorPosition--
            }
            while(true) {
                console.log('SW')
            }
        } else {
            let lightPosition = Math.abs(lightX - lightY);
            let thorPosition = Math.abs(initialTx - initialTy);
            while (thorPosition < lightPosition) {
                console.log('W')
                thorPosition++
            }
            while(true) {
                console.log('NW')
            }
        }
    } else {
        if (initialTy < lightY) {
            let lightPosition = Math.abs(lightY - lightX);
            let thorPosition = initialTx + initialTy;
            while (thorPosition < lightPosition) {
                console.log('E')
                thorPosition++
            }
            while(true) {
                console.log('SE')
            }
        }
        else {
            let lightPosition = lightY + lightX;
            let thorPosition = initialTx + initialTy;
            while (thorPosition < lightPosition) {
                console.log('E')
                thorPosition++
            }
            while(true) {
                console.log('NE')
            }
        }
    }

Thank you!

Example of a situation where you code won’t work:
initialTx, initialTy = (5, 5)
lightX, lightY = (2, 9)
Hence initialTx > lightX and initialTy < lightY.
lightPosition = 2 + 9 = 11
thorPosition = 5 + 5 = 10
Hence lightPosition > thorPosition, and your code will print ‘SW’ for ever.
As a result, Thor will move to (4, 6), (3, 7), (2, 8), (1, 9) and (0, 10) but will never reach the light at (2, 9).

I am new to the site and coding as well. I am working in PHP and tried this puzzle but I am stuck. Thor moves right past the light. Any suggestions? I tried the debug, I think and I get this : Warning: A non-numeric value encountered in the answer code at line 39

Code Below.

<?php
/**
 * Auto-generated code below aims at helping you parse
 * the standard input according to the problem statement.
 * ---
 * Hint: You can use the debug stream to print initialTX and initialTY, if Thor seems not follow your orders.
 **/

// $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
fscanf(STDIN, "%d %d %d %d", $lightX, $lightY, $initialTx, $initialTy);

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

    $directionX = '';
    $directionY = '';
    
    if ($thorX > $lightX){
        $directionX = 'W';
        $thorX -= 1;
    }
    elseif ($thorX < $lightX){
        $thorX += 1;
        $directionX = 'E';
    }
    if ($thorY > $lightY){
        $thorY -= 1;
        $directionY = 'N';
    }
    elseif ($thorY < $lightY){
        $thorY += 1;
        $directionY = 'S';
    }
       
    
    
    echo ($directionY . $directionX . "\n");

    // Write an action using echo(). DON'T FORGET THE TRAILING \n
    error_log(var_export($thorX, true )); 
}

You missed the dollar signs in the variables in these two lines:

$thorX = initialTx;
$thorY = initialTy;

Why my code is failling though its correct

#include<string.h>
#include<stdio.h>
//using namespace std;
int main(){
        int lightx,lighty,initialTx,initialTy;
        //cin>>lightx>>lighty>>initialTx>>initialTy;
        scanf("%d%d%d%d",&lightx,&lighty,&initialTx,&initialTy);
        int thorx=initialTx;
        int thory=initialTy;
        while(1){
                int remain;
                scanf("%d",&remain);
                char *dirx="";
                char *diry="";
                if(thory>lighty){
                        diry="S";
                        thory--;
                }
                else if(thory<lighty){
                        diry="N";
                        thory++;
                }
                else if(thorx>lightx){
                        dirx="W";
                        thorx--;
                }
                else if(thorx<lightx){
                        dirx="E";
                        thorx++;
                }
                printf("%s%s\n",diry,dirx);
        }
        return 0;
}

Because your code is incorrect.

  1. Do “N” and “S” match the conditions correctly in your code? Remember that (0, 0) is at the top left corner.
  2. Are the y-axis conditions and the x-axis conditions mutually exclusive?
    Your code else if(thorx>lightx) implies they are, i.e. once either the first condition or the second condition is fulfilled, the third and fourth conditions will not be checked.

(C++ - out of map)

Hi, i have read a great part of the forum on the Power of Thor but hasn’t found a solution to my problem.
I’m not english, if you don’t understand my sentences i can try making new ones more understandable.

Basically my code is : create a string called “move” before the while and put the direction in two string i concatenate at the end of the while for the cout. Like that (i try to not put all my code) :

if(initial_tx < light_x){
move_x = “E”;
initial_tx += 1;
}

I do a else if to go toward West, I decrement and I do the same for North and South (i’m starting with if and not a else if, so i can make diagonal movements).

at the end of the while I concatenate and display.
move = move_y + move_x;
cout << move << endl;

I complete the two first tests but the others send Thor out of the map •~•
If someone sees the problem, thanks.

You may consider checking:

  • Are the conditions/instructions for N and S correct? I have seen some people swap them and hence the code becomes wrong.
  • Are the variable names typed correctly? I have seen some people have the x’s and y’s in the wrong places.

Thor moves toward the light, he just goes beyond the y max :

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

He doesn’t stop when he reach the y of the light. Normally, he should stop going SW and just go W.

My variables seems to be correct.

string move = “”; // before the loop

string move_x = “”, move_y = “”; // at each start of loop

i use the x and y of Thor :
int initial_tx, initial_ty;

You may do some debugging by using cerr to print variables to the error stream so that you can check whether the variables are updated correctly.

If you still aren’t able to find out the issue, you may PM me your code and let me have a look.