What should I do to reduce run time of my code?
set in the first solution, and while i in range(n) in the second one are very expensive.

That said, thou shalt not bruteforce.

The crazy guy moves right as long as he visits the odd-populated farms, leaving a trail of even-populated farms behind. As soon as he hits the even-populated farm, he switches direction, and since now on he faces only the even-populated farms, he goes all the way to the Star 1, and the track stops there.

So, find the leftmost even-populated farm. If there is none, the guy would visit all farms, stealing one ship per farm. If there is, its index is the number of farms visited; on the way there, one ship per farm will be stolen, and on the way back another ship per farm will be stolen (except if initially there was just one ship).

This should be enough to keep you going.

As a side note, break in

    if x[i] == 0:
        if i &gt;= farm_visited: farm_visited = i+1
        break


is a bug. An empty farm should not stop him:


  he steals only one sheep (if there is any) from that farm and moves on