Solve Problem 18
Simplify Problem 18 and remove immutability