Power Of Thor - Episode 1 - Puzzle discussion

I am incredibly frustrated by this puzzle.

I declare CurrentTX and CurrentTY int variables before the while statement. I update those variables within each if statement. I have 8 if statements, one for each cardinal direction. I lose on the third challenge when Thor drops off the bottom of the map trying to go SW even though my code for going SW isn’t met because thor’s position is (18,17) and the Light’s position is (0,17) and thus CurrentTY - lightY IS NOT < 0.

else if (CurrentTX - lightX > 0 && CurrentTY - lightY < 0)

At that point he should start going east via this statement:

else if (CurrentTX - lightX > 0 && CurrentTY - lightY == 0)

But instead he keeps going SW. Again, just to be clear, I have variables for his current position and they are updated in the if statements.

It would be a lot less frustrating if you put debug statements in your program to understand what happens exactly. The stderr is made for this and to know the value of your variables and in which “if” statements the code flows would help you a lot.

Note: {∅, N, S} x {∅, E, W} = 9 possibilities. If you have 8 “if”, it won’t be enough in general.

Aries, yes I ended up solving the issue with debug statements. It ended up being an inversion of the updater code for the y position. For a puzzle that was supposed to teach conditionals, they really throw you for a loop with their decision to position 0,0 at the top left instead of bottom left.

8 Conditionals was absolutely enough though. One for each possible direction. I guess theoretically you could have one for “don’t move” but its not really necessary since none of the 8 conditionals will be met in that situation and so none of their code will fire. I mean, the provided solution in the puzzle only used 4 conditionals.

0,0 is top-left in most programming languages.

on the last case of test ur energy will not be enough to complete so u gotta do the move NE

Please provide sample output in a comment instead of a line of code, I missed it and spent ages thinking where i was wrong:sweat_smile:

you dont have to code according to the level names, it should work for all inputs within the constraints

This problem can be solved with only four ifs
Hint: consider giving incomplete outputs in each if

if initial_tx >= 1 :
if(initial_ty <= 1):
if (light_x < initial_tx and light_y < initial_ty):
print(“NW”)
if (initial_tx <=39):
if (light_x > initial_tx and light_y < initial_ty):
print(“NE”)
if (initial_ty >= 17):
if (light_x < initial_tx and light_y > initial_ty):
print(“SW”)
if (light_x < initial_tx):
print(“W”)
if (initial_tx <= 39):
if (initial_ty >= 17):
if (light_x > initial_tx and light_y > initial_ty):
print(“SE”)
if (light_x > initial_tx):
print(“E”)
if(initial_ty >= 1):
if (light_y < initial_ty):
print(“N”)
if (initial_ty <= 17):
if(light_y > initial_ty):
print(“S”)

this doesn’t work in the browser but works in the official IDE ?!?!?

If you mean visible testcases and submit with “browser” and “official IDE”: testcases differ to prevent hardcoding (which you’ve done).
A solution shall pass every possible input, not just the ones you see.

1 Like

Hello, i’m stuck on the angle problem. My thor keeps going SW despite a condition making it impossible when thor reaches a coordinate close to the light in the vertical direction:

elseif (($thorX > $lightX) && ($thorY < $lightY) && ($thorY <= 16)) {

the conditions are ignored even with the overkill 3rd condition. Thor reaches 18 while still going SW (same statement executed instead of falling back on another one)

Just to be extra safe i made sure the expected fall back was the next command:

elseif (($thorX > $lightX) && ($thorY >= $lightY)) {

and it still doesn’t work. Thor Only goes SW (first line) 18 times and die on the 3rd test.

The first two tests works just fine with the same structure.

How are Thor coordinates updated between each turn ?

Did you check out the hints of the puzzle?

When Y = 0, you’re on the top line of the map. Y goes up as you move down. Does your code reflect that?

Yes. The coordinates are updated as follow:
$thorY = $thorY + 1;
$thorX = $thorX - 1;
so in theory, if thorY < 17, ThorY cease updating beacause the following case is
elseif (($thorX > $lightX) && ($thorY >= $lightY)) {
echo (“W\n”);
$thorX = $thorX - 1;
So whevener Thor reaches the edge of the map hes supposed to quit going in the SW direction.

However on execution Thor only goes SW 18 times and dies.

The loop works fine for single direction cases.
The thor variables are duplicates of the initial variables, updated on each loop iteration.

If that part of the code is correct then the problem must be somewhere else… sounds obvious but it bears mentioning. Updating variables, nesting braces, etc are common spots to check. Try inserting some debug comments to see what your variables are doing and what parts of the code are being used.

1 Like

When I try to submit, I’m stuck on the “Loading…” screen while it computes my score, ant it never submits…any idea why this is happening? My code passes all the tests.

We had an issue with the platform. It is fixed now, you can submit again your solution.

Help please, I am stuck here. I tried the 8-if-conditions solution, but for some reason, the code below only works for cases 1 & 2 but not 3 & 4. I don’t know what went wrong!

    char direction;
    if (thorX < lightX && thorY < lightY) {
        direction = 'SE';
        thorX++; thorY++;
    }
    if (thorX < lightX && thorY > lightY) {
        direction = 'NE';
        thorX++; thorY--;
    }
    if (thorX > lightX && thorY < lightY) {
        direction = 'SW';
        thorX--; thorY++;
    }
    if (thorX > lightX && thorY > lightY) {
        direction = 'NW';
        thorX--; thorY--;
    }
    if (thorX == lightX && thorY < lightY) {
        direction = 'S';
        thorY++;
    }
    if (thorX == lightX && thorY > lightY) {
        direction = 'N';
        thorY--;
    }
    if (thorX < lightX && thorY == lightY) {
        direction = 'E';
        thorX++;
    }
    if (thorX > lightX && thorY == lightY) {
        direction = 'W';
        thorX--;
    }

    printf("%c\n", direction);

try printing a debug message like “I chose this direction: XX” in each of your 8 ifs. You’ll quickly see where the issue lies.