Here is how I ended up solving the problem from first principles, by using an algorithm from 1978.
https://rahulrav.com/blog/magic_move.html