A*Craft Puzzle discussion

No, i don’t have this problem.
With the auto-generated code, reading the inputs only takes 5 milliseconds.

A few questions :

  • does it take 2 seconds every time ?
  • did you try to remove some parts of your code (like the creation of robot objects) to see if it affects the time ?
  • the timeout of the game is 1 second but your program is allowed to print something after 2 seconds ? (usually when you exceed 10-100 ms the local arena timeouts and no more logs are printed)

If there nothing wrong with the hidden code, your AI shouldn’t take this long.

2 Likes

Then it is an issue with my code.

did you try to remove some parts of your code (like the creation of robot objects) to see if it affects the time ?

No (I was so sure that It could not be my code ha ha) I’ll try to iteratively remove each piece of the code to find which one causes the issue.
Thank you.

So actually with the auto-generated code:

class Player {

    public static void main(String args[]) {
        long start = System.nanoTime();

        Scanner in = new Scanner(System.in);
        for (int i = 0; i < 10; i++) {
            String line = in.nextLine();
        }
        int robotCount = in.nextInt();
        for (int i = 0; i < robotCount; i++) {
            int x = in.nextInt();
            int y = in.nextInt();
            String direction = in.next();
        }
        System.err.println("time elapsed " + ((System.nanoTime() - start)/1000000) +"ms");
        
        System.out.println("0 0 U 1 1 R 2 2 D 3 3 L");
    }
}

I end up with the same issue on multiple test cases.

time elapsed 2191ms
time elapsed 1784ms

System.nanoTime() returns nanoseconds, so the above result should be valid.

Hi, you should initialize your start variable just after you get the first input, not before.

1 Like

I’m an idiot… it worked thank you :slight_smile:

Apparently it’s the creation of the Scanner that takes 2s.
If you try :

    long start = System.nanoTime();
    Scanner in = new Scanner(System.in);
    System.err.println("time elapsed " + ((System.nanoTime() - start)/1000000) +"ms");
    start = System.nanoTime();
    // read inputs
    System.err.println("time elapsed " + ((System.nanoTime() - start)/1000000) +"ms");

It should print something like :
time elapsed 2129ms
time elapsed 6ms

CodinGame starts its timer after the first input has been read.
So, to have the right elapsed time, your code should be :

    Scanner in = new Scanner(System.in);
    long start = 0;
    for (int i = 0; i < 10; i++) {
        String line = in.nextLine();
        if (i == 0) {
            start = System.nanoTime();
        }
    }

PS : It explains why you were allowed to print something after 2s.
I didn’t see there was already an answer from @cg123 :slight_smile:

1 Like

test are broken on rust

Please provide details.