Python Input() Print

All of the puzzles/competitions on Codingame provide text inputs though calling “input” (assuming using Python). I’m trying to create a simulator in Python to test out solutions locally and am wondering if anybody knows how I would provide a response to a Python input() function call (e.g. from a bot agent)? Everything online seems to assume a human would be providing the input response.

Thanks!

Not sure what you want exactly but for example if you add the following on top of your script, now input in your script collects the input from the multiline string you put on top:

iter_input = iter("""COPY
PASTE
TESTCASE
HERE""".splitlines())

input = lambda: next(iter_input)

Copy the data in a separate text file, say puzzle.txt where the script is puzzle.py.
Then do (in bash):

cat puzzle.txt | puzzle.py

I was afraid my description was going to be confusing! Take two:

  1. Let’s say I’m writing and iterating on a bot for a competition, using Python. The general steps the bot needs to do are (1) call “input”, (2) decide on an action, and (3) “print” a move.

  2. If I want to simulate how the bot would do, I could create a separate (simplified) referee that feeds the bot inputs, processes the moves to forecast the next state of the game, do scoring/visualization, etc.

  3. What I’ve generally done is to have 2 blocks of code: one that lives only on the Codingame IDE that calls inputs translates it to lists/dicts for my bot (step 1), and the actual agent itself (steps 2,3). This isn’t ideal since if something breaks I have to troubleshoot both pieces (only the 2nd of which I can work with offline).

The question I have is that if the agent needs to call “input” to work with the codeingame platform, is there a way for my Python simulator to respond the input request without a workaround (and read printed text)?

If I understand the responses correctly: Pardouin’s example would be similar to my current approach (a workaround with slightly different input code required for the actual codingame site), and Nicola’s example would be tough to do with dynamic, multi-round games (writing to a txt file each round is actually the way to go?). I was hoping I was missing something simple. Hopefully that’s clearer?

For many things on here the referee is on github and you can directly run that with the SDK to test your bot.

For your specific question (writing a python simulator that interacts with your code) look into subprocess.Popen. This can be used on the simulator side to launch your code. You’ll want to use subprocess.PIPE for stdin / stdout to allow you to dynamically communicate.

1 Like

Thanks for the tip RoboStac. Unless I’ve missed something, the referees that are posted (like the one for Cultist recently) and the broader Brutal Tester tool are java oriented, so I was hoping to avoid learning a new language for now. Can you clarify what you mean by SDK?

I’ll do more research on subprocess - thanks!