Power Of Thor - Episode 1 - Puzzle discussion

Hello, can someone explain how this work

 while (1) {
        int remainingTurns; // The remaining amount of turns Thor can move. Do not remove this line.
        scanf("%d", &remainingTurns);
        char *directionX = "";   <===== How this work, why pointer to double comma cause, saving places of thor?
        char *directionY = "";
        if (thorY > lightY) {
            directionY = "N"; <===== and how this connects with this double comma N?
            thorY--;

Is there a bug in Java version? I have solved this in c# and code working ok
in the 3rd test there are 36 moves to complete the level but in Java there are only 18 moves
so you cant complete the level.

I’m able to solve the Java version of the 3rd test without any issue. There is no bug with that test.

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")