Skip to content

Commit

Permalink
09.30: implementation(실습용 로봇)
Browse files Browse the repository at this point in the history
  • Loading branch information
luckylooky2 committed Oct 1, 2024
1 parent 4bb187e commit 29b7616
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions programmers/실습용 로봇.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// 실습용 로봇 : 구현
function solution(command) {
let [x, y] = [0, 0];
const [RIGHT, LEFT, GO, BACK] = ["R", "L", "G", "B"];
command = command.split("");
const dir = [
[0, 1],
[1, 0],
[0, -1],
[-1, 0],
];
let index = 0;

for (const cmd of command) {
if (cmd === RIGHT) {
index = (index + 1) % 4;
} else if (cmd === LEFT) {
index = (index - 1 + 4) % 4;
} else if (cmd === GO) {
x += dir[index][0];
y += dir[index][1];
} else if (cmd === BACK) {
x -= dir[index][0];
y -= dir[index][1];
}
}
return [x, y];
}

0 comments on commit 29b7616

Please sign in to comment.