I was asked to solve this puzzle once during a project management interview. The solution here says "work backwards," but I found that "work recursively" was a more helpful (and instructive) way to think about it.
When A is in charge this affects....
When B is in charge this affects...
Maybe that's recursively?
solve(pirates) {..... solve(mutatedPirates) }