could not solve problem. ended up using solution from text but at least i understand how the solution works. it's quite ingenious