def calculate_reindeer_run(reindeer, duration):
    """Calculate distance travelled by reindeer in specified duration"""

    # get reindeer stats
    flight_speed = reindeer['flight_speed']
    flight_duration = reindeer['flight_duration']
    rest_period = reindeer['rest']

    # get number of rounds reinder flies and rests
    rounds = duration // (flight_duration + rest_period)
    # time spent during rounds
    time_spent = rounds * (flight_duration + rest_period)
    # distance covered during rounds
    distance_covered = rounds * flight_speed * flight_duration
    # time left after rounds
    time_left = duration - time_spent

    # whatever time is left,
    # fly for a bit (until flight duration or time left runs out)
    # and add that to the total duration
    if flight_duration <= time_left:
        distance_covered += flight_speed * flight_duration
    else:
        distance_covered += flight_speed * time_left

    return distance_covered