Solution to ZigZag problem.