We can simulate this problem by running each step of Farmer John's movement. At each step, we check Farmer John's starting position and his intended ending position, and check to see whether that overlaps Bessie's position. If it does not, then we add the full distance to a running total and move poor Farmer John back to his starting position. Otherwise, we add the distance between FJ's current position and Bessie's position to the running total, and return that total as our answer.

Here's Jonathan Paulson's code. See the comments for some more insight on what he's doing:

#include <iostream> #include <cstdlib> using namespace std; typedef long long ll; int main() { ll x, y; cin >> x >> y; ll ans = 0; ll by = 1; ll dir = 1; while(true) { // dir == 1 means Farmer John is moving to the right, and // dir == -1 means he is moving to the left. if((dir==1 && x<=y && y<=x+by) || (dir==-1 && x-by<=y && y<=x)) { // We found Bessie! ans += abs(y-x); cout << ans << endl; break; } else { // Didn't find Bessie! Add to our running total the cost of // moving 'by' units away from the start and back again. // Then multiply our next move's length by 2 and switch direction. ans += by*2; by *= 2; dir *= -1; } } }