My solution for the qualifier round of Google code jam 2013