first attempt at problem 18, this one is pretty tough as well...