There is software that does all this work. It’s used by political consultants.
It has all the possible inputs regarding population sizes, demographics, etc, etc.
The software then generates what the new congressional district borders should be to sway them to one side or the other.