풀이
김영준
// (0,0) 위치에서 시작 // 캐릭터가 처음 걸어본 길의 길이를 리턴 // 좌표평면의 경계를 넘어가는 명령어는 무시 function solution(dirs) { const direction = { L: [0, -1], R: [0, 1], D: [-1, 0], U: [1, 0] }; let location = [0, 0]; const move = []; for (const dir of dirs){ let [dy, dx] = direction[dir]; let [cy, cx] = location; let [ny, nx] = [cy + dy, cx + dx]; if(ny >= -5 && ny <= 5 && nx >= -5 && nx <= 5){ move.push(`${ny}${nx}${location[0]}${location[1]}`); move.push(`${location[0]}${location[1]}${ny}${nx}`) location[0] = ny; location[1] = nx; } } return new Set(move).size / 2; }
이종현
function solution(dirs) { let direction = { L: [-1, 0], R: [1, 0], U: [0, 1], D: [0, -1] }; let answer = [0, 0]; let route = new Set(); for (let dir of dirs) { let X = answer[0] + direction[dir][0]; let Y = answer[1] + direction[dir][1]; if (X > 5 || X < -5 || Y > 5 || Y < -5) continue; route.add("" + answer[0] + answer[1] + X + Y); route.add("" + X + Y + answer[0] + answer[1]); answer = [X, Y]; } return route.size / 2; }
박노철
function solution(dirs) { //처음 지나가는 길의 길이 //현재위치 let now =[0,0]; const move={"U":[1,0], "D":[-1,0], "R":[0,1], "L":[0,-1]} // 중복을 지우기위해 set const path =new Set(); for(let a of dirs){ const [cy,cx ]=now; const [my,mx]=move[a]; const [ny, nx]=[cy+my, cx+mx]; //좌표안이라면 to,from, form,to 둘다 넣어주고, now를 바꿔준다. if(nx>=-5 && nx<=5 && ny<=5 && ny>=-5){ path.add([cy,cx,ny,nx].join("")); path.add([ny,nx,cy,cx].join("")); now=[ny,nx]; } } // 갔던길왔던길이 저장돼서 2로 나누어줘야하낟. return path.size/2
이민희
function solution(dirs) { const directions = { 'U': [0, 1], 'D': [0, -1], 'L': [-1, 0], 'R': [1, 0] } const maxX = 5; const maxY = 5; let pos = [0, 0]; const visited = []; // 지나간 경로를 문자열로 저장 (출발x출발y도착x도착y) dirs.split('').forEach((dir) => { // 명령어에 따른 다음 좌표 const tempX = pos[0] + directions[dir][0]; const tempY = pos[1] + directions[dir][1]; // 범위를 벗어나지 않는다면 pos 위치를 다음 좌표로 이동시킨다. if (tempX <= maxX && tempX >= -maxX && tempY <= maxY && tempY >= -maxY) { // 방향 무관하므로 양방향을 모두 저장한다. visited.push(`${pos[0]}${pos[1]}${tempX}${tempY}`); visited.push(`${tempX}${tempY}${pos[0]}${pos[1]}`); pos[0] = tempX; pos[1] = tempY; } }) return new Set(visited).size / 2; // 처음 가본 길만 포함하므로 중복을 제거한다. }
박건우
function solution(dirs) { let answer = 0; const direction = { U : [1, 0], D : [-1, 0], R : [0, 1], L : [0, -1] } const memory = {}; let [y, x] = [0, 0]; for(const key of dirs){ const [dy, dx] = direction[key]; const [ny, nx] = [y + dy, x + dx]; //범위 밖으로 나가면 명령 무시 if(ny < -5 || ny > 5 || nx < -5 || nx > 5){ continue; } // 갔던 길을 memory에 저장합니다. 이때 반대방향으로 간 길도 체크해줍니다. const memoryKey1 = [y, x, ny, nx].join(" "); const memoryKey2 = [ny, nx, y, x].join(" "); // 두 길 모두 체크가 안되어있다면 처음 가본 길입니다. if(!memory[memoryKey1] && !memory[memoryKey2]){ answer++; memory[memoryKey1] = true; memory[memoryKey2] = true; } // 이동해줍니다. [y, x] = [ny, nx]; } return answer; }
박주연
function solution(dirs) { const direction = { U: [1,0], D: [-1,0], R: [0,1], L: [0,-1] } let start = [0, 0]; const visited = []; for (let i of dirs){ const move = [start[0] + direction[i][0], start[1] + direction[i][1]]; // 좌표평면의 경계를 넘어가는 경우는 continue if(Math.abs(move[0]) > 5 || Math.abs(move[1]) > 5) continue; // 경로 저장 0001, 0100 visited.push(''+start[0]+start[1]+move[0]+move[1]); visited.push(''+move[0]+move[1]+start[0]+start[1]); start = move; } console.log(visited); const set = new Set(visited); return set.size /2 }