News Archive
PhpRiot Newsletter
Your Email Address:

More information

Facebook Hacker Cup 2011 Qualification Round: the PHP code

Note: This article was originally published at Planet PHP on 11 January 2011.
Planet PHP

I wrote about how I completed the 3 exercices of Facebook's Hacker Cup 2011 qualification round in my previous posts (Double Squares, Peg Game and Studious Student) and now I'll provide the code I wrote. Since the Facebook qualification round ended, I guess it's safe to post, and will allow people who have kept their input and output to really confirm if they won or not (it seems some people receive mails saying they do, then saying they don'ta looks like the Hacker Cup was hacked together a bit too quickly).

As I said previously in the Double Squares post, the concept was not to make nice code or think for a long time, but complete the exercices as fast as possible to have a correct output before everyone else. Instead of optimizing the code and write nice long comments, I wrote code that would provide the correct output with the minimal amount of coding efforts (which was achieved by being brutal). Because of this the code is almost not commented. If you want to understand what you see, you'd better look at the appropriate posts.

The code is meant to be run from PHP command line, using the php interpretor, or using php's cgi binary (with flag -q). It should work with PHP 5.1+ (I use PHP 5.3) and will produce the output for the .txt file with the same name.

Instead of reading the number of entries in the file (first line) I ignored it and will just read each file until I reach EOF, ignoring empty lines (like the final line). There might be some ugly hacks, but it wouldn't be a Hacker Cup without some hacks.

The code, in a 23kB .zip file

Remember that this was written as fast as possible. The goal was not to make nice code, but to make code quickly. It will work and produce correct output given any valid input data.

I'll be back next week!