Fun solution to a challenging algorithmic problem.