My messy solution for Problem A 2013

First shot at this, hope to get better