Nice, I'm glad you made it to the end! I think it is really hard to use math to solve it all at once. In my opinion, the best approach is to use a bit of tries and tweaks based on the intuition that gets built. I think you nailed it :)
If you're interested in a breakdown of how I interpret the mechanic, check out the comment I left in reply to the user phlp a few comments above yours.