Problem 12 naive solution (too slow)