Problem 12 Solved: much more efficient and elegant than previous solution