Skip to content

Commit c356f87

Browse files
committed
87차 3번 문제풀이
1 parent 4a6161d commit c356f87

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

live8/test87/문제3/이상민.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
function validateDir(x, y, dir) {
2+
switch (dir) {
3+
case "U":
4+
return [x, y + 1];
5+
case "D":
6+
return [x, y - 1];
7+
case "R":
8+
return [x + 1, y];
9+
case "L":
10+
return [x - 1, y];
11+
}
12+
}
13+
14+
function solution(dirs) {
15+
const visited = new Set();
16+
let x = 0;
17+
let y = 0;
18+
19+
for (dir of dirs) {
20+
const [nx, ny] = validateDir(x, y, dir);
21+
22+
if (nx >= -5 && nx <= 5 && ny >= -5 && ny <= 5) {
23+
visited.add(`${x}${y}${nx}${ny}`);
24+
visited.add(`${nx}${ny}${x}${y}`);
25+
x = nx;
26+
y = ny;
27+
}
28+
}
29+
30+
return visited.size / 2;
31+
}

0 commit comments

Comments
 (0)