# -*- coding: utf-8 -*-
# Code for Python 2.7

from collections import defaultdict
from zipfile import ZipFile

tension = 0

#convert hh:mm string to minutes after midnight
def str_to_time(s):
    return int(s[0:2]) * 60 + int(s[3:5])

#read stop_times.txt and count length of all driving and waiting activities
with ZipFile("wien.zip", "r") as zip_file:
    with zip_file.open("stop_times.txt", "r") as stop_times_file:
        #skip first line
        stop_times_file.readline()
        
        #add differences of arrival of current row - departure of last row for all trips
        last_trip = None
        last_departure = None
        for line in stop_times_file:
            entries = line.split(",")
            if entries[0] == last_trip:
                tension += str_to_time(entries[1]) - last_departure
            last_trip = entries[0]
            last_departure = str_to_time(entries[2])
            
           
print "Tension of all driving and waiting activities:", tension

#get all minimum transfer times
Transfers = defaultdict(lambda: 0)

with ZipFile("wien.zip", "r") as zip_file:
    with zip_file.open("transfers.txt", "r") as transfers_file:
        #skip first line
        transfers_file.readline()
        
        #read minimum transfer times and convert to minutes
        for line in transfers_file:
            entries = line.split(",")
            Transfers[entries[0]] = int(entries[3])/60
            

#collect all arrivals and departures at a stop
Arrivals = defaultdict(lambda: [])
Departures = defaultdict(lambda: [])
            
with ZipFile("wien.zip", "r") as zip_file:
    with zip_file.open("stop_times.txt", "r") as stop_times_file:
        #skip first line
        stop_times_file.readline()
        
        #difficulty: first and last stop of a trip:
        last_trip = None
        last_stop = None
        last_departure = None
        for line in stop_times_file:
            entries = line.split(",")
            #do not take the arrival at the first stop of the trip
            if int(entries[4]) > 0:
                Arrivals[entries[3]].append((entries[0], str_to_time(entries[1])))
            #do not take the departure at the last stop of the trip
            if entries[0] == last_trip:
                Departures[last_stop].append((last_trip, last_departure))
            last_trip = entries[0]
            last_stop = entries[3]
            last_departure = str_to_time(entries[1])

#accumulate transfer times                       
for stop in Arrivals:
    min_transfer_time = Transfers[stop]
    for (arrival_trip, arrival_time) in Arrivals[stop]:
        for (departure_trip, departure_time) in Departures[stop]:
            #do not regard waiting activities
            if arrival_trip != departure_trip:
                #compute transfer time in [min_transfer_time, min_transfer_time + 4]
                tension += (departure_time - arrival_time - min_transfer_time) % 5 + min_transfer_time
                
print "Tension of all activities:", tension
