Solution to Google Code Jam Question