My rudimentary solution

Probably not the prettiest, but it works. Also, apologies for not using tests. Can you recommend resources for using tests for non-rails development?

Thank you for doing this contest. This is my first programming contest submission (of any kind).