There is a 'simpler' way to find flippings of range 1, but doesn't work for consecutive repeated flips
before = np.array([0,0,1,0,0,1,0,1,1,1,2,2,1,2,1,1,1,0,1,0,0])
goal = np.array( [0,0,0,0,0,0,1,1,1,1,2,2,2,1,1,1,1,1,0,0,0])
diff = np.diff(before) # find 'jumps' with range 1
diff[:-1][(diff == 0)[1:]] = 0 # correct 'right' side of 'jumps'
after = before - np.r_[0,diff] # correct flipping
plt.plot(before, linestyle='--',label='before')
plt.plot(goal, linestyle='-.',label='goal')
plt.plot(after, linestyle=':', label='after')
plt.legend();
Output

Works with bigger steps and negative 'jumps'
before = np.array([0,0,-1,0,0,1,0,1,1,1,4,4,1,4,1,1,1,0,1,0,0])
goal = np.array( [0,0, 0,0,0,0,1,1,1,1,4,4,4,1,1,1,1,1,0,0,0])
diff = np.diff(before) # find flipping with range 1
diff[:-1][(diff == 0)[1:]] = 0 # correct 'right' side of signal
after = before - np.r_[0,diff] # correct flipping
plt.plot(before, linestyle='--',label='before')
plt.plot(goal, linestyle='-.',label='goal')
plt.plot(after, linestyle=':', label='after')
plt.legend();
Output

Limits of the solution
- Consecutive repeated flips
before = [0,1,0,1,0,1,1,1,1]
goal = [0,0,0,0,1,1,1,1,1]
