Power Of Thor - Episode 1 - Puzzle discussion

This problem must remember to change the original position, otherwise it will run wrong at 3…

Perhaps your ‘move’ should be reset after each iteration of the loop

sorry but what should: “Failure: Thor wandered off the path and died (invalid position).” mean?

That means Thor has moved beyond the boundaries (top, bottom, left or right) of the grid.

after 4 if statements it no longer reads my input for a SW in c++ I’ve tried many ways to do the if statements but I cant get it to work. thank you if anyone provides a solution to this.

i moved onto this recently. i know it wont work for the first two but it wont read the south if statement.

if(initial_tx != 0 && initial_ty != light_y)
{
cout << “W” << endl;
}
if(initial_tx == light_x & initial_ty == light_y)
{
cout << “s” << endl;

            }
                if (initial_tx <= light_x & initial_ty >= light_y) 
                {
                    cout << "E" << endl;
                }
                    if (initial_tx <= light_x & initial_ty >= light_y)
                    {
                        cout << "N" << endl;
                    }

this was my old code:

if(initial_tx == 0 & initial_ty == 4)
{
cout << “SW” << endl;
}
if(initial_tx == 31 & initial_ty == 4)
{
cout << “W” << endl;

            }
                if (initial_tx == 5 & initial_ty == 4) 
                {
                    cout << "E" << endl;
                }
                    if (initial_tx == 31 & initial_ty == 17)
                    {
                        cout << "N" << endl;
                    }

Your code says if initial_tx is equal to light_x and initial_y is equal to light_y then output “s”. Questions:

  1. Why is it “s” instead of “S”?
  2. Why should Thor move south if Thor’s position is the same as the light position?

Similarly, please check the conditions you have in your other if-blocks. For example, why should both directions x and y be checked before deciding whether E and N should be output?

Please also note that when you output endl, it ends the current turn. You won’t be able to output NE / SE / NW / SW that way.

it checks both so that in later solutions it wont get confused and i wont have to adjust the code as much. maybe… that’s the idea i had.

this is also what i have adjusted it to:
if(tx > lx & ty < ly)
{
if(tx >= lx & tx != 0 & ty < ly & ty !=17)
cout << “W” << endl;
}
if(tx < lx & ty == ly)
{
if(tx == lx & ty != 31)
cout << “S” << endl;
}
if (tx < lx & ty == ly)
{
cout << “E” << endl;
}
if (tx == lx & ty > ly)
{
cout << “N” << endl;
}

One way a correct code may be structured would be like this:
• check the difference in y-coordinates, and decide whether the output should contain N, S or neither.
• check the difference in x-coordinates, and decide whether the output should contain E, W or neither.
• combine the two results together and output it as a single string.

Remember that each of N, S, E and W is related to the difference in either x-coordinates or y-coordinates but not both, and your code should be able to handle compound directions of NE, NW, SE and SW when necessary too.

im not sure how i would go about that. arent i already checking x and y with the if statements?

that is very complicated im not familiar with too many algorithms for something so complex.

Your code doesn’t cater for compound directions of NE, NW, SE and SW. It’ll be too slow in some situations if Thor is able to move in N/S/E/W only.

i agree with you. i was trying to implement SW but it wouldnt read the next condition to stop going south west and only going west.

May I continue the discussion with you in private message?

yes you can

why would this not work?! beginner here so don’t judge too harshly

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

game loop

while True:
remaining_turns = int(input()) # The remaining amount of turns Thor can move. Do not remove this line.
thor_x=initial_tx
thor_y=initial_ty
# Write an action using print
# To debug: print(“Debug messages…”, file=sys.stderr, flush=True)
if light_x>thor_x & light_y>thor_y:
thor_x+=1
thor_y+=1
code=“NE”
elif light_x==thor_x & light_y<thor_y:
thor_y-=1
code=“N”
elif light_x<thor_x & light_y<thor_y:
thor_x-=1
thor_y-=1
code=“NW”
elif light_x==thor_x & light_y<thor_y:
thor_y-=1
code=“S”
elif light_x>thor_x & light_y>thor_y:
thor_x+=1
thor_y-=1
code=“SE”
elif light_x<thor_x & light_y>thor_y:
thor_x-=1
thor_y-=1
code=“SW”
elif light_x>thor_x & light_y==thor_y:
thor_x+=1
code=“E”
elif light_x<thor_x & light_y==thor_y:
thor_x-=1
code=“W”

# A single line proiding the move to be made: N NE E SE S SW W or NW
print(code)

In Python, we use and instead of & for a logical and. See:

>>> 4<5 & 6<7
False # What?
>>> 4<5 and 6<7
True # OK!

This apparent oddity is not caused by the semantics of &, but by the operator precedence. Indeed, (4<5) & (6<7) evaluates to True.

Nonetheless, it is true that the and operator is the right choice for logical and.

Yes, 4<5 & 6<7 means 4<(5&6)<7 and 5&6 is 4.

What is wrong with my code :

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.
    if initial_tx < light_x and initial_ty < light_y:
        print("SE")
    elif initial_tx < light_x and initial_ty > light_y:
        print("NE")
    elif initial_tx > light_x and initial_ty < light_y:
        print("SW")
    elif initial_tx > light_x and initial_ty > light_y:
        print("NW")
    elif initial_tx > light_x and initial_ty == light_y:
        print("W")
    elif initial_tx < light_x and initial_ty == light_y:
        print("E")
    elif initial_ty > light_y and initial_tx == light_x:
        print("N")
    elif initial_ty < light_y and initial_tx == light_x:
        print("S")

You still have to update Thor’s position yourself.