post

Some Tips for Google Code Jam

The 2013 Google Code Jam has now started! I would like to share some tips that I think are very useful for competing in GCJ.

Change download location

In GCJ, you have to download the input files and solve them quickly. It is a great time saver if the input files are downloaded right to your working directory after you click “Solve X-Small” / “Solve X-Large”.

You can change the location in your browser setting. For example, in Chrome:

download_location

tee

It is very important to check the integrity your output files before submitting them. The fact that your solution passes the example cases does not mean that your solution will pass the official input files easily. For example, if you find that your output file contain negative numbers, whereas the answer is supposed to be non-negative, then it means you have overflow error somewhere in your solution.

However, checking the output file by opening it manually can be rather time-consuming. You can use tee to automatically check it. tee is a UNIX command that writes to a file and the standard output simultaneously. The basic usage is

./solution < input_file | tee output_file

The above command will run your solution using input from input_file, and output the answer to both output_file and the standard output (the screen). Furthermore, if you flush the output after printing the answer of each test case, this will work in real-time; i.e., as soon as your solution finishes solving a test case, the answer will be immediately displayed to the screen. This way you can estimate how long your solution will finish solving all test cases in your machine. In addition, as soon as your solution output strange answer, you can immediately stop the execution of your program and debug it.

This is different from the traditional way. You have to wait until your solution solves all test cases before you can check the sanity your output file.

tee only works in UNIX. For Windows users, try using wintee (I haven’t tested it, though).

Edit: Felix Halim pointed out that non-UNIX users can simulate tee’s behavior by printing the answer twice: to the output file and to the standard error as well, like this:

cout << "Case #" << tc << ": " << res << endl;
cerr << "Case #" << tc << ": " << res << endl;

This works well because the test case output in GCJ is usually small (like a single integer), so you only have to copy one single printing statement.

Automate everything

You only have 4 minutes for the small input and 8 minutes for the large input. Time is very precious in GCJ, so I like to automate everything. I wrote a small C++ program to automate the process of running my solution on the correct input files and producing output files. Here are the source code if you are interested.

Edit: Because GCJ now supports more than one type of large input files for each problem, I have modified the source code a bit.

#include 
#include 
#include 

char buf[1024];

int fail()
{
	printf("Usage: gcj <problem> (small [<attempt>] | large [<type>])\n");
	printf("Examples:\n");
	printf("    gcj A small\n");
	printf("    gcj A small 1\n");
	printf("    gcj B large\n");
	printf("    gcj C large 1\n");
	return 0;
}

int main(int argc, char* argv[])
{
	if (argc >= 3 && !strcmp(argv[2], "small"))
	{
		int attempt = 0;
		if (argc == 4)
			sscanf(argv[3], "%d", &attempt);
		else if (argc > 4)
			return fail();
		sprintf(buf, "./%s < %s-small-attempt%d.in | tee %s-small-attempt%d.out",
                     argv[1], argv[1], attempt, argv[1], attempt);
		system(buf);
	}
	else if (argc >= 3 && !strcmp(argv[2], "large"))
	{
		if (argc == 3)
			sprintf(buf, "./%s < %s-large.in | tee %s-large.out",
                         argv[1], argv[1], argv[1]);
		else if (argc == 4)
			sprintf(buf, "./%s < %s-large-%s.in | tee %s-large-%s.out",
                         argv[1], argv[1], argv[3], argv[1], argv[3]);
		else
			return fail();
		system(buf);
	}
	else
		return fail();
}

So, for example, here is how I work on problem A:

  • Write the solution as A.cpp.
  • Build it into A.
  • Download the small input file.
  • Run “gcj A small” and watch the tee output.
  • Submit the output file and source code.

For more advanced automation, you can use codejam-commandline beta tool developed by Googlers. However, I find the above tips are sufficient for me.

C++ big integer data type

For C++ users, you don’t have to worry again if the problem requires you to work in arbitrary-precision integer data type (a.k.a. bignum or big integer). You can use the GMP library as it is free and publicly available. You don’t have to switch to Python/Java in contest now! For more reference, you can consult vexorian’s blog post: http://vexorian.blogspot.com/2010/05/google-code-jam-qualification-rounds.html.

View results by country

One of the downside of GCJ is that you cannot have too many friends to watch on the friends scoreboard. Usually, you want to watch the performance of coders in your country. You can use a nice tool developed by Ahmed Aly: Google Code Jam Tools. It can show the results of previous contests for any country.

Any other tips? Please share 😀

About Ashar Fuadi

Ashar Fuadi is a competitive programmer from University of Indonesia. He loves to code, especially for TopCoder SRM, Codeforces, and ICPC.
Follow Ashar on Google+ and Twitter.

Comments

  1. Hi ! I would like to share my google code jam API which also provides a command line application for automatic input downloading / output submission :).

    Take a look at the github repository https://github.com/Faylixe/googlecodejam-client !

Speak Your Mind

*