Messy code for problem 50.  I think this is solved, but it runs too long.  Darn.