본문 바로가기
Coding Test/Programmers

[Programmers] (Summer/Winter Coding(~2018)) Lv 2. 방문 길이

by song.ift 2023. 3. 3.

https://school.programmers.co.kr/learn/courses/30/lessons/49994

 

프로그래머스

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

programmers.co.kr

 

const solution = (dirs) => {
    const [x, y, BOUNDARY] = [0, 1, 5];
    const inArea = (command, pos) =>
        (command === 'L' && pos[x] > -BOUNDARY)
        || (command === 'R' && pos[x] < BOUNDARY)
        || (command === 'D' && pos[y] > -BOUNDARY)
        || (command === 'U' && pos[y] < BOUNDARY)

    return [...dirs].reduce((acc, command) => {
        if (!inArea(command, acc.pos)) return acc;

        const prev = [...acc.pos];
        (command === 'L') ? --acc.pos[x] : (command === 'R') ? ++acc.pos[x] : 0;
        (command === 'D') ? --acc.pos[y] : (command === 'U') ? ++acc.pos[y] : 0;

        if (!acc.history[[prev, acc.pos]] && !acc.history[[acc.pos, prev]]) {
            acc.history[[prev, acc.pos]] = acc.history[[acc.pos, prev]] = true;
            ++acc.move;
        }
        return acc;
    }, { pos: [0, 0], history: {}, move: 0 }).move;
}

GitHub : https://github.com/developeSHG/Algorithm-Baekjoon_Programmers/tree/main/%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%A8%B8%EC%8A%A4/lv2/49994.%E2%80%85%EB%B0%A9%EB%AC%B8%E2%80%85%EA%B8%B8%EC%9D%B4

 

GitHub - developeSHG/Algorithm-Baekjoon_Programmers: 백준 and 프로그래머스 소스코드

백준 and 프로그래머스 소스코드. Contribute to developeSHG/Algorithm-Baekjoon_Programmers development by creating an account on GitHub.

github.com

 

댓글