Simplify Problem 18 and remove mutability