[ヒント]2019年KACAブラインド公募第2次オフライン-Elevator--③コード


  • サーバを実行中...
  • シミュレーションの結果を確認しています...
  • シミュレーション結果を表示すると
    開発者ツールを開き、consoleウィンドウで
    let btn = document.querySelector('#ts')
    let timer = setInterval(function() { btn.setUp(); show(btn.value); }, 1000)
    
    clearInterval(timer)  <= 도중에 멈추고 싶을 때 입력
    上記のコマンドを入力すると、エレベーターの運行状況をより簡単に確認できます!
  • コアロジック
  • 2019 KACA BLIND公募第2回オフラインコード試験問題解説Collective controlと同様
    私の場合、論理を簡略化するために、
    エレベーターが行く方向に乗客がいなくても
    まず最下層または最上層に到着します.
    方向を変える戦略を選んだ.
    途中でエレベーターに乗りたい乗客がいるかもしれません.
    それほど悪い戦略ではないようです.
    短時間で論理を作成できます
    いいtrade-offだと思います.
    (そしてライアンタで1897タイムスタンプを記録!!)
    def get_commands(verbose=False):
        with open('settings.json', 'r') as json_file:
            settings = json.load(json_file)
            token = settings['token']
    
        with open('condition.json', 'r') as json_file:
            condition = json.load(json_file)
            timestamp = condition['timestamp']
            elevator_directions = condition['elevator_directions']
    
        pprint(f'timestamp: {timestamp}, token: {token}')
    
        oncalls_data = api.get_oncalls()
        calls = {}
        for call in oncalls_data['calls']:
            id = call['id']
            calls[id] = Call(**call)
        elevators = {}
        for elevator in oncalls_data['elevators']:
            id = elevator['id']
            passengers = []
            for call in elevator.pop('passengers'):
                passengers.append(Call(**call))
            direction = elevator_directions[id]['direction']
            elevators[id] = Elevator(**elevator, passengers=passengers, direction=direction)
    
        if verbose:
            pprint(calls)
            pprint(elevators)
    
        bldg_floors = defaultdict(deque)
        for id, call in calls.items():
            bldg_floors[call.start].append(call)
    
        if verbose:
            pprint(bldg_floors)
    
        commands = []
        for id, elevator in elevators.items():
            command = None
            call_ids = []
            exit_ids = [call.id for call in list(filter(lambda _call: _call.end == elevator.floor, elevator.passengers))]
            bldg_floor = bldg_floors[elevator.floor]
    
            if elevator.status == STOPPED:
                if exit_ids:  # 내릴 사람들이 있을 때
                    command = OPEN
                elif bldg_floor and len(elevator.passengers) < MAX_PASSENGERS:  # 기다리는 사람들이 있고 태울 수 있을 때
                    command = OPEN
                else:  # 이번 층에서는 볼일 없으니 가던 길 감
                    command = elevator.direction
            elif elevator.status == OPENED:
                if exit_ids:  # 내릴 사람들이 있을 때
                    command = EXIT
                    call_ids = exit_ids
                elif bldg_floor and len(elevator.passengers) < MAX_PASSENGERS:  # 기다리는 사람들이 있고 태울 수 있을 때
                    command = ENTER
                    while bldg_floor and len(elevator.passengers) < MAX_PASSENGERS:
                        call = bldg_floor.popleft()
                        call_ids.append(call.id)
                        elevator.passengers.append(call)
                else:  # 이번 층에서는 볼일 다 봤으니 문 닫음
                    command = CLOSE
            elif elevator.status == UPWARD:
                if exit_ids:  # 내릴 사람들이 있을 때
                    command = STOP
                elif bldg_floor:  # 기다리는 사람들이 있을 때
                    command = STOP
                elif elevator.floor == MAX_FLOORS:  # 꼭대기 층에 도착했을 때
                    command = STOP
                    condition['elevator_directions'][elevator.id]['direction'] = DOWN
                    with open('condition.json', 'w') as json_file:
                        json.dump(condition, json_file, indent=4)
                else:  # 가던 길 감
                    command = UP
            elif elevator.status == DOWNWARD:
                if exit_ids:  # 내릴 사람들이 있을 때
                    command = STOP
                elif bldg_floor:  # 기다리는 사람들이 있을 때
                    command = STOP
                elif elevator.floor == 1:  # 1 층에 도착했을 때
                    command = STOP
                    condition['elevator_directions'][elevator.id]['direction'] = UP
                    with open('condition.json', 'w') as json_file:
                        json.dump(condition, json_file, indent=4)
                else:  # 가던 길 감
                    command = DOWN
    
            if command in [ENTER, EXIT]:
                command = {'elevator_id': elevator.id, 'command': command, 'call_ids': call_ids}
            else:
                command = {'elevator_id': elevator.id, 'command': command}
            commands.append(command)
    
        pprint(commands)
        return commands
  • 完全コード
    https://github.com/keeprainy/my---kakao-recruit-2019-blind-2nd-elevator