Power Of Thor - Episode 1 - Puzzle discussion


#703

How come the following code fails on testcase 2 and 3? The diagonal movement works but when the inputs are in the correct state to move either west or east, the next movement is still a diagonal.

using System;
using System.Linq;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;

/**
 * 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.
 **/
class Player
{
    static void Main(string[] args)
    {
        string[] inputs = Console.ReadLine().Split(' ');
        int lightX = int.Parse(inputs[0]); // the X position of the light of power
        int lightY = int.Parse(inputs[1]); // the Y position of the light of power
        int initialTX = int.Parse(inputs[2]); // Thor's starting X position
        int initialTY = int.Parse(inputs[3]); // Thor's starting Y position

  string d;
        // game loop
        while (true)
        {
           d = "";
            int remainingTurns = int.Parse(Console.ReadLine()); // The remaining amount of turns Thor can move. Do not remove this line.

            // Write an action using Console.WriteLine()
            // To debug: Console.Error.WriteLine("Debug messages...");
            
           
            
          if (initialTX > lightX && initialTY == lightY) //5 7 8 9
            {
                Console.Error.WriteLine("Debug messages...");
                d = "W";
            }
            else if (initialTX < lightX && initialTY == lightY)
            {
                Console.Error.WriteLine("Debug messages...");
                d = "E";
            }


            if (initialTY > lightY && initialTX == lightX)
            {
                d = "N";
            }
            else if (initialTY < lightY && initialTX == lightX)
            {
                d = "S";
            }


            //

            
           if (initialTX < lightX && initialTY > lightY) 
            {
                d = "NE";
            }
            else if (initialTX > lightX && initialTY > lightY)
            {
                d = "NW";
            }

            if (initialTX < lightX && initialTY < lightY)
            {
                d = "SE";
            }
            else if (initialTX > lightX && initialTY < lightY)
            {
                
                d = "SW";
            } 
            
            
                
                
                


            // A single line providing the move to be made: N NE E SE S SW W or NW
            Console.WriteLine(d);
        }
    }
}

#704

Do you update Thor’s coordinates?


#705

I did not :). I thought it was done automatically. I updated his coordinates and it worked, thx.


#706

I am facing trouble with the last two test cases, any insight?:expressionless:
I realize that I may have to deal with diagonal movements, right?


#707

Yes you have to. You can output things like “NE” / “NW” / “SE” / “SW” !


#708

He may but he may not have to.


#709

My code run pretty well on visual studio
I’m a new user, therefor I can not upload image
but it run thor(x,y) to the same location as the light once ^.^
Hope developer can fix it, then I can get the whole score;
here is my chickened code ~~

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

/**
* 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.
**/
int main()
{
	string direction;
	int lightX; // the X position of the light of power
	int lightY; // the Y position of the light of power
	int initialTX; // Thor's starting X position
	int initialTY; // Thor's starting Y position
	cin >> lightX;
	cin.ignore();
	cin >> lightY;
	cin.ignore();
	cin >> initialTX;
	cin.ignore();
	cin >> initialTY;
	cin.ignore();
	// game loop
	while (1) {
		int remainingTurns; // The remaining amount of turns Thor can move. Do not remove this line.

							// Write an action using cout. DON'T FORGET THE "<< endl"
							// To debug: cerr << "Debug messages..." << endl;
		cin >> remainingTurns;

		for (int i = 0; i < remainingTurns; i++) {
			if (initialTY == (lightY - 1)) {
				initialTY = lightY;
			}

			if (initialTX > lightX) {
				--initialTX;
			}
			else if (initialTX < lightX) {
				++initialTX;
			}
			else {}


			if (initialTY > lightY) {
				--initialTY;
			}
			else if (initialTY < lightY) {
				++initialTY;
			}
			else {}


			if ((initialTY == lightY) && (initialTX < lightX)) {
				direction = "E";
			}
			else if ((initialTY == lightY) && (initialTX > lightX)) {
				direction = "W";
			}
			else if ((initialTY < lightY) && (initialTX == lightX)) {
				direction = "S";
			}
			else if ((initialTY > lightY) && (initialTX == lightX)) {
				direction = "N";
			}
			else if ((initialTY < lightY) && (initialTX < lightX)) {
				direction = "SE";
			}
			else if ((initialTY > lightY) && (initialTX < lightX)) {
				direction = "NE";
			}
			else if ((initialTY > lightY) && (initialTX > lightX)) {
				direction = "NW";
			}
			else if ((initialTY < lightY) && (initialTX > lightX)) {
				direction = "SW";
			}

			// A single line providing the move to be made: N NE E SE S SW W or NW
			cout << direction << endl;
			//cout << lightX << "  " << lightY << "  " << initialTX << "  " << initialTY;
		}
	}

}

#710

Could anyone explain to me why this solution works for test 1,2,3 only, but not for 4?

 class Player {

public static void main(String args[]) {
    Scanner in = new Scanner(System.in);
    int lightX = in.nextInt(); // the X position of the light of power
    int lightY = in.nextInt(); // the Y position of the light of power
    int initialTX = in.nextInt(); // Thor's starting X position
    int initialTY = in.nextInt(); // Thor's starting Y position
    int grobX = 0;
    int grobY = 0;
    int bewegen=0;
    String richtung="";

    // game loop
    while (true) {
        int remainingTurns = in.nextInt(); // The remaining amount of turns Thor can move. Do not remove this line.

        // Write an action using System.out.println()
        // To debug: System.err.println("Debug messages...");
        //x-Richtung
        if (lightX-initialTX<0)
        {
            grobX=-1;
            initialTX=initialTX-1;
        }
       else if (lightX-initialTX>0)
       {
           grobX=1;
           initialTX=initialTX+1;
       }
      else
      {
            grobX=0;    
      }
      
      //y-Richtung
        if (lightY-initialTY<0)
        {
            grobY=-10;
            initialTY=initialTY-1;
        }
       else if (lightY-initialTY>0)
       {
           grobY=10;
           initialTY=initialTY+1;
       }
      else
      {
            grobY=0;    
      }
        bewegen=grobX+grobY;
        
        switch(bewegen){
    case -10:
        richtung="N";
        break;
    case -9:
        richtung="NE";
        break;
    case 1:
        richtung="E";
        break;
    case 11:
        richtung="SE";
        
   case 10:
        richtung="S";
        break;
    case 9:
        richtung="SW";
        break;
    case -1:
        richtung="W";
        break;
    case -11:
        richtung="NW";        
        
        break; 
        }
        
        // A single line providing the move to be mhave obade: N NE E SE S SW W or NW
        System.out.println(richtung);
    }
}

}


#711

First, move. THEN update coordinates.


#712

0,0 is top-left (furthest Northwest). Is that what your code is assuming?


#713

Thanks for your answer. Yes, (0,0) is top-left here, too.

I changed the updates of the coordinates from the “if else if” lines to the switch cases. Thor’s movement changed, but he is still too slow to pass test #4.

Walking diagonally in test #3:https://www.codingame.com/replay/381790729
Walking NOT diagonally in test #4: https://www.codingame.com/replay/381790887


#714

I finally found my mistake:

In the switch cases, I forget to put “break;” in the SE case, so the direction was overwritten to S.


#715

Great puzzle! It was very fun. One thing: Mjölnir was spelled wrong.


#716

(In the english version.)


#717

how to solve the optimal angle testcase?


#718

Hello!

I am using C++ for this puzzle. When I was trying to assign and print letters for the directions, I originally used char data type and it did not work. When I switched to string data type, the code worked fine. Why can I not print char data type for the directions?


#719

I can’t understand why the variable ‘remaining_turn’ should be written again and again in the infinite while loop. To work it as a constraint, it should be initialized only once at the outside of the while loop.


#720

You mean read ?


#721

Superb episode and awesome discussion


#722

Superb episode and awesome discussion I just love it