https://school.programmers.co.kr/learn/courses/30/lessons/49994
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;
}
'Coding Test > Programmers' 카테고리의 다른 글
[Programmers] Lv 2. 덧칠하기 (0) | 2023.03.07 |
---|---|
[Programmers] (2018 KAKAO BLIND RECRUITMENT) Lv 2. [1차] 프렌즈4블록 (0) | 2023.03.06 |
[Programmers] (2018 KAKAO BLIND RECRUITMENT) Lv 2. [3차] 파일명 정렬 (0) | 2023.03.03 |
[Programmers] Lv 2. 롤케이크 자르기 (0) | 2023.02.27 |
[Programmers] (2021 KAKAO BLIND RECRUITMENT) Lv 2. 메뉴 리뉴얼 (0) | 2023.02.25 |
댓글