I previously mentioned using donations to shelter income. His house offers some additional opportunities.
I think that a huge fraction of the cost went to cool custom gadgets. If Gates set up a company to research/develop those gadgets, he can deduct the "losses" when said company doesn't manage to sell them. Yes, he should pay for the gadgets themselves, but they're cheap compared to the NRE.
There are some ways to do tax-free roll-over to fund companies, so the deductions would shelter other income.
Even if Gates had to pay cap gains to fund the company that developed the gadgets, he's way ahead if the deductions are at ordinary income rates. If he both pays and deducts at cap gains rates, he's just out cap gains on the gadgets themselves.
However, like I said, I don't know how much tax planning Gates does. He has over $60B, so avoiding cap gains on $40M is like someone with $2M avoiding cap gains on a cheap used car. Then again, he may have set up a system 20 years ago to pull out money tax-free every year. Putting $40M through it may be just SOP.