```python
def leg_gain(start: int, end: int) -> int:
    """Returns the height gained on one leg: the rise when the ground goes up,
    and zero when it goes down or stays level."""
    if start > end:
        return -end + start
    else:
        return end - start

def climb_gain(heights: list) -> int:
    """Adds the gains of every leg of a walk, given the heights in order.
    A walk of fewer than two heights gains nothing."""
    if not heights:
        return 0
    return sum(leg_gain(heights[i], heights[i + 1]) for i in range(len(heights) - 1))
```