[problem 10] simplify and optimize code to 2sec