Found a solution for Problem 18. I still need to write the dynamic programming version though.