Power Of Thor - Episode 1 - Puzzle discussion

Thanks it helped. It should be println and I used print instead.

I probably never played this kind of game. When I read the description I thought I only need to output the final move. Secondly when I checked the posted solution I had no idea how the remainingTurns was used, how the while loop got finish. I was completely puzzled. Any one tell me what I missed?

The while loop is an endless loop and will not finish. This is intentional. The program itself will finish as soon as you solved the problem (or run into an error, or run out of time).
You task is to give out all movements needed to reach the light.

Regarding remainingTurns, follow the instructions: You can ignore this data but you must read it.
It’s not needed at all.

Best way to start is, take pen and paper, draw an X for the light, and T for Thor’s position on the given coordinates. Then trace a path from T to X and write it down, split into 1-sized steps: “N”, “E”, “NW”, etc…
Now all you need to do is write a program that can output this step by step :slight_smile:

2 Likes

1.Thanks for the reply. How does the program terminate?
2. “N”, “E” counted as 2 moves, “NE” counted as 1 move?

  1. It ends when Thor reaches the goal.
  2. Yes and “NNNEEE” too.

My code passes test 1, test 2 and test 3.
In test 3 Thor walks diagonelly, but in test 4 he walks south first and then east so he doesn’t have enough energy.
I don’t see where the difference is between test 3 and test 4.

You can make Thor do 1 move diagonally by outputing ‘NE’ / ‘NW’ / ‘SE’ / ‘SW’.

For some test case the way is far shorter by doing diagonal moves.

To verify if two integers are equal, you should use “===” not “==”. “==” verify only if two values are equal, not considering the type (integer versus string).

It means that :
“2” == 2; // true
2 == 2; // true
“2” === 2; // false
“2” === “2”; // true
2 === 2; // true

1 Like

Considering origin is depicted at NW corner with Y axis pointing downwards, it would be less confusing if the Y coordinate has negative values. instead of imaging the coordinate plane upside down (from a desktop/laptop view perspective).

I am trying to figure out thor’s puzzle and what I have seen in the forums in c++ that there is a bug and can’t be solved. I wanted to see if that was still the case or has this been fixed?

My C++ solution passes both IDE tests and validators on submit.

can you offer any advice i keep going past the y direction. i set a if else for everything. N, S, E, W, NE, NW, SE, and SW. but my Y keeps adding

You have to update Thor’s position yourself after each move.
print("N"); thor_y = thor_y - 1;

Hi all
Does anyone know in C++, what is the difference between type char and char*?
I fail to run the code when i just using the type char?

Thank you in advance.

#include
#include
#include
#include
using namespace std;
int main()
{
int lightX;
int lightY;
int TX;
int TY;
cin >> lightX >> lightY >> TX >> TY;

while (1) {
    int remainingTurns;
    cin >> remainingTurns; cin.ignore();
    
    char* dirX="";
    char* dirY="";
    
    if (lightX>TX)
    {
        dirX="E";
        TX++;
    }
    else if (lightX<TX)
    {
        dirX="W";
        TX--;
    }
    if (lightY>TY)
    {
        dirY="S";
        TY++;
    }
    else if (lightY<TY)
    {
        dirY="N";
        TY--;
    }

    cout << dirY << dirX << endl;
}

}

Do you know what’s a pointer?

Not really.
But from my understanding, it should be okay even without the pointer.
As inside the loop, each iteration i will re-declare and reset the variable dirX and dirY to null.

In java, you have to use System.out.println and not System.out.print

"\n" is the delimiter for each action provided.

I wrote the code in Python.
And the test cases #: 2,3 & 4 It’s failure.
And I can’t see the mistakes. Can someone help me?

import sys
import math


light_x, light_y, initial_tx, initial_ty = [int(i) for i in input().split()]

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

    # Write an action using print
    # To debug: print("Debug messages...", file=sys.stderr)
    if x == light_x and y != light_y:
      if y < light_y:
        print("N")
        y += 1
      else:
        print("S")
        y -= 1
    elif y == light_y and x != light_x:
      if x < light_x:
        print("E")
        x += 1
      else :
        print("W")
        x -= 1
    elif x < light_x and y < light_y:
        print("NE")
        y += 1
        x += 1
    elif x < light_x and y > light_y:
        print("SE")
        y -= 1
        x += 1
    elif x > light_x and y < light_y:
        print("NW")
        y += 1
        x -= 1
    elif x > light_x and y > light_y:
        print("SW")
        y -= 1
        x -= 1

Remember than in computer graphics (as opposed to geometry), the origin (0,0) position is at the top-left of the screen and the y-coordinate increases in the downward direction. This is stated in the Rules section of the puzzle.

1 Like

Thanks a lot! I appreciate your time.