Code Royale timeouts on input

Honestly, I have no clue what happened. I was working on a different part of the code (towards the end of the file) when suddently during a test (which I run often), I get a timeout (failed to provide output in time). With some debugging, I managed to figure out the line that timed out. It was the input line that is already provided by default… I checked using the python time module to see if I actually ran out of time but right before the input I’m at 3ms out of 50… Here’s the part:

    for i in range(num_units):
        # unit_type: -1 = QUEEN, 0 = KNIGHT, 1 = ARCHER
        x, y, owner, unit_type, health = [int(j) for j in input().split()] # Thats the line
        if unit_type == -1 and owner == 0:
            queen = Unit(x, y, owner, unit_type, health)
        else:
            units.append(Unit(x, y, owner, unit_type, health))

It actually goes through the loop just fine on the first time (to find my queen) but fails on the second looping. This happens on the first turn, so there are only 2 queens as units, but what happens to the other queen? It works fine on mine but not the other…

are you sure you’re correctly outputting a command at first turn? If not, the system sees you’re trying to read the second turn’s input without having output something at turn 0 so it thinks you timeout.

Python likes to buffer stderr so debug messages aren’t always printed immediatly - this often makes it look like it’s hanging somewhere where it isn’t.
When printing make sure you tell it to flush (by either changing your print commands as below or calling sys.stderr.flush() after each print).
print(“message”, file=sys.stderr, flush=True)

1 Like

Tried that, doesn’t change a thing…

This is the first turn. I can’t output anything cause it hangs before doing so.

This will happen if you try to read more inputs than are being given; for example, maybe your program is waiting for the 6th input and there are only 5. Not saying that’s what’s happening here since I don’t know, but it’s something to investigate.

On a similar note, is is possible you read too many inputs earlier in the code?

1 Like