A similar problem and corresponding solution can also be found here:
https://github.com/LogtalkDotOrg/logtalk3/blob/master/examples/puzzles/jam_thief.lgt
Like the solution posted by Kaarel, is possible to request a justification/explanation for the solution found.