The AI is moving around randomly. Its goal is to get to the green without touching red or wasting 30 turns. When it fails, it remembers the path it took to get to that red space/out of turns and will not copy that path again. It repeats that until it eventually makes it to green.