import datetime

input = open("trace_mario.txt").read()
# input = open("trace_mario_example.txt").read()

input = input.splitlines()

def conpute_distance(pos1, pos2):
    """from math import radians, cos, sin, asin, sqrt

    # convert decimal degrees to radians 
    lon1, lat1, lon2, lat2 = map(radians, [pos1["long"], pos1["lat"], pos2["long"], pos2["lat"]])

    # haversine formula 
    dlon = lon2 - lon1 
    dlat = lat2 - lat1 
    a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
    c = 2 * asin(sqrt(a)) 
    r = 6371 # Radius of earth in kilometers. Use 3956 for miles. Determines return value units.

    dalt = pos2["alt"] - pos1["alt"]
    return sqrt((c * r )**2 + dalt**2) * 1000"""
    from math import pi, cos, sqrt

    r = 6371 * 1000

    dlat = pos2["lat"] - pos1["lat"]
    dlong = pos2["long"] - pos1["long"]

    dist_lat = (2 * pi * r) / 360 * dlat
    dist_long = (2 * pi * r * cos((pos1["lat"] + pos1["lat"])/2/360*2*pi)) / 360 * dlong
    dist_alt = pos2["alt"] - pos1["alt"]

    return sqrt((dist_lat**2+dist_long**2)+dist_alt**2)

data = []

for line in input:
    lat, long, alt, horodate = line.split(" ")
    data.append({"lat":float(lat), "long":float(long), "alt":float(alt), "time":datetime.datetime.strptime(horodate, "%H:%M:%S")})

distance = 0
duree = datetime.datetime.min
montee_descente = "M" #Montée par défaut

for i in range(1, len(data)):
    #Check altitude
    if data[i]["alt"] < data[i-1]["alt"]:
        montee_descente = "D"
    elif data[i]["alt"] > data[i-1]["alt"]:
        montee_descente = "M"

    #If descente
    if montee_descente == "D":
        duree = duree + (data[i]["time"] - data[i-1]["time"])
        distance = distance + conpute_distance(data[i], data[i-1])

duree = duree - datetime.datetime.min

print(distance, distance/duree.total_seconds())
