Solution for code challenge at https://www.getlocalmotion.com/code-challenge. Still suboptimal, though. My algorithm skills are getting rusty...