The problem with your code is that you've chosen the wrong solution. Ranked voting will forever go through the cycle "plurality is broke" ==> "wow, IRV is cool, let's do that" ==> "crap, that outcome was fucked, this ranked voting stuff reeks, back to plurality." ==> "plurality is broke" ==> ad infinitum.
The pragmatic solution is approval voting. Simple, combinable (combining results from districts is trivial summation), explainable and adequate. Yep, adequate. Not perfect, not fantastic but adequate and not intrinsically broken. I'm sure you've seen the analyses that expose the instability and dangers of IRV. Maybe you want to do something more Condorcetish and that is where you are going with your math. The problem is that less than 1% of the population will ever fully grok how it works and it will ultimately fail because joe public won't understand it and will reject it when it stumbles, even if it only appears to stumble.
Similarly to the original topic, technical folk are so often perfectionists but with flawed direction. The fanciness of the GUI gets focused on at the expense of performance, features get added without first understanding the challenges the users actually face. All resulting in the genesis of the complaint for this topic - slow, unpleasant to use and annoying software.
This behavior of falling in love with the wrong thing runs so deep in the tech world it is often invisible to all but a few grounded pragmatists. Kinda sad I think. How do you get folks to take a genuine, critical look at the things they've gotten attached to?