python problem 12 solution is way too slow