Third homework assignment for Udacity class CS334

It works and generates a correct result when posted online but the code
is currently really ugly and extremely inefficient. Still needs some
major clean-up and basic optimizations. Current runtime online is  about
4 ms.