optimization for snake direction code