#2 Solver for problem 18