First off, leaving money in a 529 account is not that bad, since you may always change the beneficiary to most any blood relative. So if you have leftovers, you don't HAVE to pay the 10% penalty if you have a grandchild, for instance, that can use it for college (part of the original law) or even for private school tuition (added in 2018 tax law). But if you would rather have the money out, then you need a strategy to get it out that is tax efficient.
My prescription for managing a situation like this is not to pay directly out of the 529 account, but instead calculate your cost of education up-front and withdraw that money at the beginning of the school year. You can keep it in a separate account, but that's not necessary. The amount you withdraw should be equal to what the education costs, which may be estimated by taking the budget that the school publishes minus grants and scholarships. You should have all of those numbers before the first day of school. This is amount $X.
During the year, write all the checks out of your regular account. At the end of the school year, you should expect to have no money left in the account. I presume that the budget is exactly what you will spend. If not, you might need to make a few adjustments, but this answer will presume you spend exactly $X during the fall and the spring of the next year.
In order to get more out of the 529 without paying penalties, you are allowed to remove money without penalty, but having the gains taxed ($y + $z). You have the choice of having the 529 funds directed to the educational institution, the student, or yourself. If you direct the funds to the student, the gains portion would be taxed at the student's rate. Everyone's tax situation is different, and of course there is a linkage between the parent's taxes and student's taxes, but it may be efficient to have the 529 funds directed to the student. For instance, if the student doesn't have much income, they might not even be required to file income tax. If that's the case, they may be able to remove an amount, $y, from the 529 account and still not need to file. For instance, let's say the student has no unearned income, and the gains in the 529 account were 50%. The student could get a check for $2,000, $1,000 would be gains, but that low amount may mean the student was not required to file.
Or if it's more important to get more money out of the account, the student could remove the total amount of the grants plus scholarships ($y + $z). No penalty would be due, just the taxes on the gains. And at the student's tax rate (generally, but check your own situation).
Finally, if you really want the money out of the account, you could remove a check ($y + $z + $p). You'd pay tax on the gains of the sum, but penalty of 10% only on the $p portion.
This answer does not include the math that goes along with securing some tax credits, so if those credits still are around as you're working through this, consider this article (which requires site sign-up). In part, this article says:
How much to withdraw -
... For most parents, it will be 100% of the beneficiary’s qualified higher education expenses paid this year—tuition, fees, books, supplies, equipment, and room and board—less $4,000. The $4,000 is redirected to the American Opportunity Tax Credit (AOTC),...
When to withdraw it -
Take withdrawals in the same calendar year that the qualified expenses were paid. ....
Designating the distributee -
Since it is usually best that the Form 1099-Q be issued to the beneficiary, and show the beneficiary’s social security number, I prefer to use either option (2) or (3) [ (2) a check made out to the account beneficiary, or (3) a check made out to the educational institution]
What about scholarships? -
The 10 percent penalty on a non-qualified distribution from a 529 plan is waived when the excess distribution can be attributed to tax-free scholarships. While there is no direct guidance from the IRS, many tax experts believe the distribution and the scholarship do not have to match up in the same calendar year when applying the penalty waiver.
If you're curious about timing (taking non-penalty grants and scholarship money out), there is this link, which says you "probably" are allowed to accumulate grants and scholarship totals, for tax purposes, over multiple years.