Algorithm/Implementation

[프로그래머스 PCCP 모의고사 1번] 실습용 로봇

킹우현 2023. 11. 11. 16:19

def solution(command):
    answer = []
    # 로봇은 입력된 명령에 따라 x, y좌표를 이동
    # G : 바라보는 방향으로 한칸 전진
    # B : 바라보는 방향으로 한칸 후진

    # 위, 오른, 아래, 왼
    dx = [0,1,0,-1]
    dy = [1,0,-1,0]

    direction = 0
    x,y = 0, 0

    command = list(command)

    for com in command:
        if com == "R":
            direction = (direction + 1)%4
        elif com == "L":
            if direction > 0:
                direction -= 1
            else:
                direction = 3
        elif com == "G":
            x += dx[direction]
            y += dy[direction]
        elif com == "B":
            x -= dx[direction]
            y -= dy[direction]
    
    return [x,y]