Problem 12 solution