본문 바로가기
문제 풀이/Programmers

[Programmers/Python] Lv2. 방문 길이

by 망고 ෆ 2024. 6. 27.
문제
 

프로그래머스

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

programmers.co.kr

 

 

 

풀이
def solution(dirs):
    x,y = 0,0
    next_x,next_y = 0,0
    answer = set()
    dir_dict = {"U":[0,1],
                "D":[0,-1],
                "R":[1,0],
                "L":[-1,0]
               }
    for i in dirs:
        for j in dir_dict:
            if i == j:
                next_x = x+dir_dict[i][0]
                next_y = y+dir_dict[i][1]
                if (-5<=next_x<=5) and (-5<=next_y<=5):
                    answer.add(((x,y),(next_x,next_y)))
                    answer.add(((next_x,next_y),(x,y)))
                    x,y = next_x, next_y

    return len(answer)//2

댓글