this is cool! i love exercises like this. it's always cool how close the real world matches the physics, in most cases.
i think for this to be accurate for the kind of riding i do you'd need a way to break the ride into a few chunks or "types" of riding. the key factor is how often you're going any given speed since the power required doesn't vary linearly with speed. using the standard formula, here's a really modest and simple example.
the "ride with one hill" is a 13 mile ride with a single 528 foot tall hill at 10% up, and 5% down. we pedal with the same power (or same total motor+rider power) for the entire ride, resulting in one speed on the flat, a different speed going up, and a different speed going down.
the ride with the hill takes longer because the air resistance scales exponentially going down the hill. since we're inputting the same power, for more time, we do more work - in this case about 10 percent more, from just one hill! the orange cells are input here
but.... what if it's just a really shallow out and back with the same amount of climbing? now the penalty is negligible because we're not going much faster on the downhill.
in a truly brutal ride, with steep ascents and descents, what probably happens is that we don't pedal on the descents. here's an example :
now, even without wasting our power downhill where it hardly makes a difference, we still did as much work as the first hill, but our average speed dropped even further!
one approach to this would be to have the user select a "ride profile" - flat, small hills, shallow gradient, big hills, or something, and either apply some factors or do some proportional modeling. personally, i think these 5-20% potential differences in work are worth taking into account. my examples above are also for a pretty racy setup, an aero road bike with the rider in a decent position. change it to flat bars and sitting upright and those differences are going to skyrocket.