세용용용용 2023. 5. 20. 13:49

나의 코드

def solution(bridge_length, weight, truck_weights):
    answer = 0
    torock_list = []
    torock_time_list = []

    while True:
        # 다리를 건너고 있는 트럭이 없고 대기 트럭이 없으면 멈춰!!!!!
        if len(torock_list) == 0 and len(truck_weights) == 0:
            break
       
        # 초는 계속 흘러가야 되니까 증감
        answer += 1

        # 트런 시간 리스트가 존재하면 (즉, 건너고 있는 트럭 리스트가 있으면)
       
        index = 0
        while index < len(torock_time_list):
            torock_time_list[index] += 1
            if torock_time_list[index] > bridge_length:
                torock_time_list.pop(0)
                torock_list.pop(0)
            else:
                index += 1

        # 대기 트럭이 있고 건너고 있는 트럭 무게합과
        # 대기 트럭 인덱스 0을 더한 값이 제한 무게를 넘지 않으면
        # 건너고 있는 트럭 리스트에 삽입
        # 트런 시간 리스트에도 삽입 (1초로)
        if len(truck_weights) > 0 and sum(torock_list) + truck_weights[0] <= weight:
            torock_list.append(truck_weights.pop(0))
            torock_time_list.append(1)
    print(answer)
    return answer

solution(2, 10, [7, 4, 5, 6])