Here's what mathematics has to say about this:
If we allow arbitrary gerrymandering (i.e., with arbitrarily convoluted shapes) then we could take it to the mathematical extreme and assign every single voter to one of the n = 8 districts individually (and with our knowledge of their voting preferences). With a total of a voters for party A and b voters for party B (we can ignore any non-voters; and since this is the USA, we ignore any additional parties), we can partition these into n districts where each has more voters of the first party than the second if and only if a is at least b+n: Simply assign approximately b/n B-voters to each of the districts and exactly one more A-voters, and finally distribute any remaining A-voters arbitrarily.
If a<b+n, we clearly cannot make all districts favour A. But we can do it with n-1 districts (as long as a is at least n-1 and n is at least 2): Partition the a A-voters into n-1 parts (so perhaps you take approximately a/(n-1) per district). For good measure, add the same number minus one of B-voters to each of these n-1 districts (so that each is won by A with one marginal vote). Finally make the nth district from the remaining b-a+n-1 B-voters.
So much for the mathematical limitations on what is possible. We run into practical problems such as
In the second case, if A-voters are in a significant minority, the last district with the left-over B-voters may need to be much larger than the other districts. Such discrepancy may perhaps be legally ruled out.
Assigning voters individually leads to shapes that are not only weird, but in fact highly disconnected. This may be ruled out
Aiming for only one marginal vote to win a district risks failure of the attempt in case of a single person forgetting to vote
We do not actually know the exact voting behaviour of everyone
Nevertheless, every step towards a more realistic/practical gerrymandering strategy merely requires a larger "safety gap" between the a and b than the mathematical minimum of n in order make all n districts favour party A. Depending on local demographic circumstances, a voting population of millions may still allow a "valid" solution even when the difference of a and b is only in the order of about 1%.