알고리즘/프로그래머스

[Python] 주차 요금 계산

dding96 2023. 1. 21. 22:11

https://school.programmers.co.kr/learn/courses/30/lessons/92341

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

# 주차 요금 계산

import math

def solution(fees, records):
    answer = []
    car_history = {}
    
    car_list = [i.split() for i in records]
    car_list.sort(key= lambda x : x[1])
    
    # 차 번호와 출입 시간이 기록된 딕셔너리
    for car in car_list:
        if car[1] not in car_history:
            car_history[car[1]] = [car[0].split(':')]
        else:
            car_history[car[1]].append(car[0].split(':'))
    
    # 각 차량이 총 주차한 시간을 분으로 표시
    for car, time in car_history.items():
        total_time = []
        total_h = 0
        total_m = 0
        
        if len(time) % 2 != 0:
            time.append(['23','59'])
            
        for i in range(len(time)-1):
            if i % 2 == 0:
                h = (int(time[i+1][0]) - int(time[i][0]))
                m = (int(time[i+1][1]) - int(time[i][1]))
                if m < 0:
                    h -= 1
                    m += 60
                    total_h += h
                    total_m += m
                else:
                    total_h += h
                    total_m += m
        
        total_h *= 60
        
        fee = 0
        total_time = total_h + total_m
        if total_time < fees[0]:
            fee = fees[1]
            answer.append(fee)
            
        elif total_time >= fees[0]:
            fee += fees[1]
            extra_time = total_time - fees[0]
            extra_fee = math.ceil(extra_time / fees[2])*fees[3]
            fee += extra_fee
            answer.append(fee)
        
    return answer