This is my solution to the first problem.