본문 바로가기

알고리즘

카카오 2022 주차요금계산

파이썬

from math import ceil

def solution(fees, records):
    answer = []
    
    record_map = records_split(records=records)
    cars_nums = list(record_map.keys())
    cars_nums.sort()

    for car in cars_nums:
        times = list(record_map.get(car))
        sumTime = 0
        for i in range(0, len(times), 2):
            if len(times) % 2 == 1 and i == len(times)-1:
                sumTime += (1439 - times[i])
            else :
                sumTime += (times[i+1] - times[i])
                
        answer.append(calulatePay(sumTime, fees))
        
    return answer

def records_split(records):
    records_dic = dict()
    
    for record in records:
        time, carNum, inOut = record.split()
        time = convertMin(time)
        
        if records_dic.get(carNum) :
            records_dic.get(carNum).append(time)
        else:
            records_dic[carNum] = [time]

    return records_dic

def convertMin(time):
    h, m = list(map(int, time.split(":")))
    mTime = h*60 + m
    
    return mTime

def calulatePay(sumTime, fees):
    pay = fees[1]
    if sumTime <= fees[0]:
        return pay
    else :
        pay += ceil((sumTime-fees[0])/fees[2])*fees[3]
    return pay
    
fees = [120, 0, 60, 591]
records = ["16:00 3961 IN","16:00 0202 IN","18:00 3961 OUT","18:00 0202 OUT","23:58 3961 IN"]

print(solution(fees=fees, records=records))