Power Of Thor - Episode 1 - Puzzle discussion

Hey, friends!I am new to coding and JS.Could someone explain why this code doesn’t work for easy and optimal angels?This is my second time solving this task.For the first time I solved it correctly with if,else if, else.Then I decided to find an other way but it didn’t work.

var inputs = readline().split(’ ');
const LX = parseInt(inputs[0]); // the X position of the light of power
const LY = parseInt(inputs[1]); // the Y position of the light of power
const TX = parseInt(inputs[2]); // Thor’s starting X position
const TY = parseInt(inputs[3]); // Thor’s starting Y position
let tx=TX, ty=TY;

while (true) {
const remainingTurns = parseInt(readline());
let dirX=‘’, dirY=‘’;

if(tx!=LX){
tx=(tx>LX)?tx–:tx++;
dirX=(tx>LX)?‘W’:‘E’;
};

if(ty!=LY){
ty=(ty>LY)?ty–:ty++;
dirY=(ty>LY)?‘N’:‘S’;
};
console.log(dirY+dirX);
}

Two issues:

  1. You determine dirX and dirY AFTER trying to modify tx and ty. That is similar to determining where to move AFTER you have moved. The order is wrong.
  2. You have used – and ++ after the variables. That will make assignment go first (e.g. ty = ty) before incrementing or reducing the variables. That means tx and ty gets unchanged. You may take a look at this article for reference:
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Increment

You would find using console.error useful in helping you to debug, e.g. adding this line after let dirX='', dirY='';:
console.error(tx + ' ' + ty);
It prints out the values of the variables so that you know where they are different from your expected values.

By the way, please format your code properly on this forum by using the </> button in the formatting toolbar.

Hope this helps.

Hi what’s wrong with my code ?

while (true) {
const remainingTurns = parseInt(readline()); // The level of Thor’s remaining energy, representing the number of moves he can still make.
const routeT = ‘’;

if ( LX < TX && LY > TY) {
   routeT = 'SW';
} else if ( LX > TX && LY < TY ) {
    routeT = 'NE';
} else if  (LY < TY && LX < TX) {
    routeT = 'NO';
} else if ( LY < TY && LX > TX ) {
    routeT = 'SE';
} else if ( LX == TX && LY > TY ) {
    routeT = 'S';
}else if ( LX == TX && LY > TY ) {
    routeT = 'N';
} else if ( LY == TY && LX > TX ) {
    routeT = 'W';
} else if ( LY == TY && LX > TX ) {
    routeT = 'O';
}

You don’t update Thor’s coordinates.

1 Like

I updates like this, but it still wrong :sob:

const remainingTurns = parseInt(readline()); // The level of Thor’s remaining energy, representing the number of moves he can still make.
let routeT = ‘’;

let positionTX = TX;
let positionTY = TY;

if ( LX < TX && LY > TY) {
   routeT = 'SW';
   positionTX--;
   positionTY++;
} else if ( LX > TX && LY < TY ) {
    routeT = 'NE';
    positionTX++;
    positionTY--;
} else if  (LY < TY && LX < TX) {
    routeT = 'NW';
    positionTX--;
    positionTY--;
} else if ( LY > TY && LX > TX ) {
    routeT = 'SE';
    positionTX++;
    positionTY++;
} else if ( LY > TY ) {
    routeT = 'S';
    positionTY++;
}else if (  LY < TY ) {
    routeT = 'N';
    positionTY--;
} else if (  LX > TX ) {
    routeT = 'E';
    positionTX++;
} else if (  LX < TX ) {
    routeT = 'W';
    positionTX--;
}

Hello evrybody ! I dont understand my code I got this for easy angle : Informations :

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

My code : } else if ( LY > TY && LX > TX ) {
routeT = ‘SE’;
positionTX++;
positionTY++;

If someone can help me…

31 > 16 but 16 < 18 …
your code is not for this case

that’s my all code :

while (true) {
    const remainingTurns = parseInt(readline()); // The level of Thor's remaining energy, representing the number of moves he can still make.
    let routeT = '';

    let positionTX = TX;
    let positionTY = TY;

    if ( LX < TX && LY > TY) {
       routeT = 'SW';
       positionTX--;
       positionTY++;
    } else if ( LX > TX && LY < TY ) {
        routeT = 'NE';
        positionTX++;
        positionTY--;
    } else if  (LY < TY && LX < TX) {
        routeT = 'NW';
        positionTX--;
        positionTY--;
    } else if ( LY > TY && LX > TX ) {
        routeT = 'SE';
        positionTX++;
        positionTY++;
    } else if ( LY > TY ) {
        routeT = 'S';
        positionTY++;
    }else if (  LY < TY ) {
        routeT = 'N';
        positionTY--;
    } else if (  LX > TX ) {
        routeT = 'E';
        positionTX++;
    } else if (  LX < TX ) {
        routeT = 'W';
        positionTX--;
    }
    

    // Write an action using console.log()
    // To debug: console.error('Debug messages...');


    // A single line providing the move to be made: N NE E SE S SW W or NW
    console.log(routeT);
}

I don’t understand what’s wrong :confused:

Hi,

You are never updating the position of Thor, since all your checks are using TX & TY and since they are never updated it’s like Thor is always at the starting position.

1 Like

How could you do this ? :expressionless: I’m lost

Just make sure TX & TY are defined as “let” and not “const” and simply replace all the positionTX by TX and positionTY by TY, you should also drop the lines creating positionTX & positionTY as you don’t need these variables.

1 Like

idk what happend with my code it tells me that : Timeout your program did not provide an input in due time. Earth was destroyed!
can you pls explain

I’ve sent you a PM. Please send your code to me there and I’ll take a look.

The 3rd and 4th tests cannot be done
Test 1 starts Thor 26 places away and gives you 26 steps to succeed
Test 2 starts Thor 13 places away and gives you 13 steps to succeed
Test 3 starts Thor 31 places away but only gives you 14 steps
Test 4 starts Thor 36 places away but only gives you 18 steps
Or have I missed something?

Test 3 “Easy angle” starts with 44 remaining turns. It’s possible to complete it in 31 turn using “diagonal” movement (“SE”, “NE”, “SW”, “NW”).

Test 4 “Optimal angle” starts with 36 remaining turns and none of them are spare, i.e. it can’t be solved in under 36 turns.

You are correct, just realised that I was using initalTx & initialTy as Thors current positionrather than his starting position.

Thank you for your prompt reply

i made this code but thor only moves up and down why

import sys
import math

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

# light_x: the X position of the light of power
# light_y: the Y position of the light of power
# initial_tx: Thor's starting X position
# initial_ty: Thor's starting Y position
light_x, light_y, initial_tx, initial_ty = [int(i) for i in input().split()]

# game loop
while True:
    remaining_turns = int(input())  # The remaining amount of turns Thor can move. Do not remove this line.

    dif_y = light_y - initial_ty    #Y Anteil des Vektors berechnen
    dif_x = light_x - initial_tx    # same für X
    #straight = math.sqrt(dif_y*dif_y+dif_x*dif_x) # berechnen der länge des Vektors
    #schräge bewegungen
    if dif_y < 0 & dif_x < 0:   
        print ("NW")

    if dif_y > 0 & dif_x < 0:
        print ("SW")

    if dif_y > 0 & dif_x > 0:
        print ("SE")

    if dif_y < 0 & dif_x > 0:
        print ("NE")

    #gerade Bewegungen
    if dif_y == 0 & dif_x > 0:
        print ("E")

    if dif_y == 0 & dif_x < 0:
        print ("W")

    if dif_y < 0 & dif_x == 0:
        print ("N")

    if dif_y > 0 & dif_x == 0:
        print ("S")


I don’t know why when i use JS all is done (100%) but in Dart (with the same code) only 50%

People, help me find an error writing in C ++.
I ran all the tests successfully, but as a result I still get an error at Easy angle … here is my code … I already broke all my eyes while trying to find an error

#include
#include
#include
#include

using namespace std;

/**

  • 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.
    **/
    string direction_x, direction_y;
    int tx, ty, distantion_x, distantion_y;
    int main()
    {
    int light_x=32; // the X position of the light of power
    int light_y=17; // the Y position of the light of power
    int initial_tx=0; // Thor’s starting X position
    int initial_ty=0; // Thor’s starting Y position
    cin >> light_x >> light_y >> initial_tx >> initial_ty; cin.ignore();

    tx=initial_tx;
    ty=initial_ty;

    // game loop
    while (1) {
    int remaining_turns; // The remaining amount of turns Thor can move. Do not remove this line.
    cin >> remaining_turns; cin.ignore();

     // Write an action using cout. DON'T FORGET THE "<< endl"
     // To debug: cerr << "Debug messages..." << endl;
     distantion_x=light_x-tx;
     distantion_y=light_y-ty;
    
     if(tx>=0)
     {
     if (distantion_x<0){
     direction_x="W";
     tx--;
     }
     }
     else
     direction_x="";
    
     if(tx<=39)
     {
     if (distantion_x>0){
     direction_x="E"; 
     tx++;
     }
     }
     else
     direction_x="";
     
     if(ty>=0)
     {
     if (distantion_y<0){
     direction_y="N"; 
     ty--;
     }
     }
     else
     direction_y="";
     
     if(ty<17)
     {
     if (distantion_y>0){
     direction_y="S"; 
     ty++;
     }   
     }
     else
     direction_y="";
    
      
    
     cerr<<endl<<"distantion x: "<<distantion_x<<" distantion y: "<<distantion_y;   
     cerr<<endl<<"tx->"<<tx<<" "<<direction_x<<" ,ty->"<<ty<<" "<<direction_y;
     // A single line providing the move to be made: N NE E SE S SW W or NW
     cout <<direction_y<< direction_x<< endl;
    

    }
    }

Please make use of the </> button on the formatting toolbar to format your code properly, if you don’t want others to break their eyes too :sweat_smile: I tried to read your code but there was not proper indentation and also some bits of it were broken, so I gave up.

You may watch the replay to have an idea of where things may go wrong. Go to MY REPORT, then click DETAILS (see the screenshot below):