my solution to problem D