문제 설명

컴퓨터공학과에서는 실습용 로봇을 이용해서 로봇 프로그래밍을 학습합니다. 실습용 로봇은 입력된 명령에 따라 x좌표와 y좌표로 표현되는 2차원 좌표 평면 위를 이동합니다. 하나의 명령은 하나의 문자로 주어지며 각 명령어에 따라 로봇이 수행하는 일은 다음과 같이 네 종류입니다.

  • 'R': 로봇이 오른쪽으로 90도 회전합니다.
  • 'L': 로봇이 왼쪽으로 90도 회전합니다.
  • 'G': 로봇이 한 칸 전진합니다.
  • 'B': 로봇이 한 칸 후진합니다.

명령어는 각각의 명령들이

모인 하나의 문자열로 주어지며, 차례대로 수행됩니다.

로봇은 처음에 (0, 0) 위치에 +y 축을 향하여 놓여 있습니다.

다음 그림은 번호 순서대로 명령어 "GRGLGRG"의 과정을 보여줍니다.

!https://grepp-programmers.s3.ap-northeast-2.amazonaws.com/files/production/012648ea-a3bc-43c2-a34b-20d8602a51da/그림1.png

!https://grepp-programmers.s3.ap-northeast-2.amazonaws.com/files/production/652fad6c-33c1-41e4-96f0-ed4b2bbba496/그림2.png

!https://grepp-programmers.s3.ap-northeast-2.amazonaws.com/files/production/3ba19b8c-1d67-4c80-a372-bd7e1891a4ef/그림3.png

!https://grepp-programmers.s3.ap-northeast-2.amazonaws.com/files/production/91bc3d05-41bb-4698-bc5e-ddd9b37c21d3/그림4.png

!https://grepp-programmers.s3.ap-northeast-2.amazonaws.com/files/production/98444606-26b7-45ce-8fb0-771c14a7e974/그림5.png

!https://grepp-programmers.s3.ap-northeast-2.amazonaws.com/files/production/2e901864-518f-4168-bc85-0a3983e62ebd/그림6.png

!https://grepp-programmers.s3.ap-northeast-2.amazonaws.com/files/production/da86dda4-d637-4790-afbe-2dffaf1a0d9e/그림7.png

!https://grepp-programmers.s3.ap-northeast-2.amazonaws.com/files/production/8aa8bdfc-ef62-4d1a-9686-a176502ce4c3/그림8.png

로봇에 입력된 명령어를 순서대로 담고 있는 문자열 command가 주어집니다. 로봇이 주어진 명령어들을 순서대로 모두 수행한 뒤 도착한 최종 위치의 좌푯값 x, y를 순서대로 배열에 담아서 return 하도록 solution 함수를 완성해주세요.


제한사항

  • 1 ≤ commad의 길이 ≤ 1,000,000
  • command는 'R', 'L', 'G', 'B'으로 구성된 문자열입니다.
  • command에 들어있는 문자 하나하나가 각 명령을 나타내며, 문자열에 먼저 등장하는 명령을 먼저 수행해야 합니다.

입출력 예

command result

"GRGLGRG" [2, 2]
"GRGRGRB" [2, 0]

입출력 예 설명

입출력 예 #1

  • 문제 예시와 같습니다.

입출력 예 #2

  • 로봇이 이동한 좌표는 (0, 0) → (0, 1) → (1, 1) → (1, 0) → (2, 0) 입니다.

문제풀이

def solution(command):
    answer = [0,0]
    # 0: 정면, 1: 우측. 2: 후면, 3: 좌측
    direct = 0
    for c in command:
        if c == 'R':
            if direct + 1 < 4:
                direct += 1
            else:
                direct = 0
        elif c == 'L':
            if direct - 1 >= 0:
                direct -= 1
            else:
                direct = 3
        
        elif c == 'G':
            if direct == 0:
                answer[1] += 1
            elif direct == 1:
                answer[0] += 1
            elif direct == 2:
                answer[1] -= 1
            else:
                answer[0] -= 1
        else:
            if direct == 0:
                answer[1] -= 1
            elif direct == 1:
                answer[0] -= 1
            elif direct == 2:
                answer[1] += 1
            else:
                answer[0] += 1
        
    return answer
코코자