Power Of Thor - Episode 1 - Puzzle discussion

compute dx,dy where Thor + {dx, dy} = light

dx+dy is the manhattan distance, optimal solution takes min(dx, dy) + |dx-dy| = max(dx,dy) steps to reach solution, because each dx,dy step can be paired to reduce the cost by 1 leaving |dx-dy| remaining cardinal direction steps to go

then construct strings using a zipper:

NNNNNNNN
EEEEE

becomes
NE NE NE NE NE N N N

which easily generalizes to all 4 possible combinations

1 Like
    My code isn't working for 3rd and 4th cases. It does not change its direction when x or y coord. becomes equal to that of the light. Please guide me!!
       if( TX>LX) 
     {
         if(TY<LY) cout<<"S"<<endl;
         else if(TY>LY) cout<<"N"<<endl;
         else cout<<"W"<<endl;
     }
    else if(TX<LX)
    {
        if(TY<LY) cout<<"S"<<endl;
        else if(TY>LY) cout<<"N"<<endl;
        else cout<<"E"<<endl;
    }
    else if(TX==LX)
    {
        if(TY<LY) cout<<"S"<<endl;
        else cout<<"N"<<endl;
    }

How does the main loop get the updated var TX, TY? I donā€™t get it.

1 Like

why doesnā€™t this code work?

if(TX == TY && TY < LY)
System.out.println(ā€œNā€);
if(TX == TY && TY > LY)
System.out.println(ā€œSā€);
if(TX < TY && TY == LY)
System.out.println(ā€œWā€);
if(TX < TY && TY < LY)
System.out.println(ā€œNWā€);
if(TX < TY && TY > LY)
System.out.println(ā€œSWā€);
if(TX > TY && TY == LY)
System.out.println(ā€œEā€);
if(TX > TY && TY < LY)
System.out.println(ā€œNEā€);
if(TX > TY && TY > LY)
System.out.println(ā€œSEā€);

2 Likes

With this, you print more than one command per round!

Actually no, its heuristic is quite hermetic, but all cases are mutually exclusive (on the other hand, they donā€™t cover all possibilities). That being said, thatā€™s the only thing he got rightā€¦ Itā€™s amazing to see how such a simple problem as going from A to B can lead to such convoluted algorithm. Does the actual problem description has changed by specifying that Thor is sailing a ship and that the wind blows from (0, 0)? Because I canā€™t see any other reason to explain than the direction to take could actually depend of the north pole origin locationā€¦

so what am I exactly supposed to change?

This topic is now 52 messages long, including 3-4 differents solutions to do it properly. Please try to read the whole topic properly and then if you still need to, ask a specific question that wasnā€™t asked before.

EDIT: ok so the topic was heavily modded (including by myself) so most of the previous solutions arenā€™t there. Still, read carefully, people are giving precious advises

My code is breaking on tests 3 and 4 and for some reason Thor keeps walking on the same direction (SW instead of W / NE instead of E)

Hereā€™s the part of my code that should be running for test 3, not sharing the rest of the code so mods donā€™t delete this but you can probably have an idea of the rest of it

if (TX > LX){ 
                if (TY < LY){
                    cout << "SW" << endl;
                } else {
                    if (TY > LY){ 
                        cout << "NW" << endl;
                    } else{
                        cout << "W" << endl;
                    }
                }   
            } else {
                if (TY < LY){
                    cout << "S" << endl;
                } else {
                    cout << "N" << endl;
                }
            }

You have to update TY and TX yourself.

2 Likes

I made a code to keep my Thor between the given X and Ys but now it runs out of energy at the 3th puzzle. What am I forgetting?

Next time iā€™ll read the initialization and per loop information properly. I automatically assumed TX & TY would be per loop values.

An issue that I personally ran into and it seems a lot of others here ran into is a misinterpretation of this line in the problem:

Line 1: 4 integers LX LY TX TY. (LX, LY) indicate the position of the light of power. (TX, TY) indicate Thorā€™s starting position.

The key word there is ā€œstartingā€. Thorā€™s position will NOT automatically update to his ā€œcurrentā€ position. You must include his current position as part of your program. The way that I managed to do that was that I incremented and/or decremented TX and/or TY every time Thor moved one step. I hope that helps everyone. :smile:

2 Likes

am i to assume that it is necessary to calculate
and reinterpret a slope value into the Cardinal direction input,

in order to complete the 3rd and 4th tests?

I canā€™t believe I didnā€™t see that one. Thanks for the help!!!

Iā€™ll be interested to see an actual implementation of what you are suggesting. I was too busy figuring out where the variables were getting their updates to pay real attention to otherā€™s solutions. :wink:

It doesnā€™t help that the console makes it look like the TX and TY vars are being updated as Thor is moving. This ā€˜bugā€™ has actually helped me in other challenges because if it is not in the per-lopp input, then you track it yourself.

Perhaps a slight update to the challenge description text will help others out.

1 Like

Iā€™m having some problem, canā€™t get 100%.
When I click on ā€œRun all Testsā€ it works perfectly, I have all the " green Checked "
But when I click on ā€œSubmitā€, I get the first two tests wrong, and Thor has only 2 energy.
Anyone could give me a hands up ?

If I remember right you should only ever use the amount of energy equivalent to the Manhattan Distance which is also the shortest distance to the power of light. Since there are no obstacles this can easily be found blind. To be honest, I completely ignore the energy and just find the shortest path from the beginning.

Any hints on how to get the ā€˜updatedā€™ positions?
Iā€™m trying to learn Python. Can get past the first 2 screens but canā€™t get it to change his mind once he starts moving.
Thanks!