Solution to second puzzle is not optimal, it definately should be O(1) space, but I didn't want to think about it.