working solution for problem 9
started on problem10, running cripplingly slow