I think this has been my favorite problem so far.
I wrote a helper method "cycle" that would move $x left or right and made for an easy translation from the problem description to code.
Part B was a nice surprise for a lazy Sunday morning :)
We're a place where coders share, stay up-to-date and grow their careers.
We strive for transparency and don't collect excess data.