풀이
김영준
function solution(keyinput, board) { const width = Math.floor(board[0] / 2); const height = Math.floor(board[1] / 2); let x = 0; let y = 0; for (let p of keyinput){ switch (p){ case "left" : if(-width < x) x--; break; case "right" : if(width > x) x++; break; case "up" : if(height > y) y++; break; case "down" : if(-height < y) y--; break; } } return [x, y]; } function solution(keyinput, board) { const answer = [0, 0] // 시작 위치 const direction = { "up": [0, 1], "down": [0, -1], "left": [-1, 0], "right": [1, 0] } const width = Math.floor(board[0] / 2); //board의 width값 const height = Math.floor(board[1] / 2); // board의 height값 keyinput.forEach((key) => { const [dx, dy] = direction[key]; const newX = answer[0] + dx; const newY = answer[1] + dy; if(newX >= -width && newX <= width && newY >= -height && newY <= height ){ answer[0] = newX; answer[1] = newY; } }) return answer }
이종현
function solution(keyinput, board) { const direction = { 'up': [0, 1], 'down': [0, -1], 'left': [-1, 0], 'right': [1, 0] }; const row = Math.floor(board[0] / 2); const col = Math.floor(board[1] / 2); let answer = [0, 0]; for (let i = 0; i < keyinput.length; i++) { const input = keyinput[i]; answer[0] += direction[input][0]; answer[1] += direction[input][1]; if (Math.abs(answer[0]) > row && answer[0] < 0) answer[0] = -row; if (Math.abs(answer[1]) > col && answer[1] < 0) answer[1] = -col; if (answer[0] > row) answer[0] = row; if (answer[1] > col) answer[1] = col; } return answer; }
박노철
function solution(keyinput, board) { //board의 크기는 항상 홀수 , 만약 9이면 -4,-3,-2,-1,0,1,2,3,4이다. 시작은 [0,0], 범위를 넘어가면 무시된다 . let x=0; let y=0; //범위를 계산하기 편하게 [board[0],board[1]]=[(board[0]-1)/2,(board[1]-1)/2] const xRange=[board[0]*-1, board[0]]; const yRange=[board[1]*-1, board[1]]; // 각입력에 맞추어서 for(let i of keyinput){ i==="right"? x+=1:i==="left"? x-=1:i==="up"? y+=1:y-=1; x<xRange[0]?x+=1:x>xRange[1]?x-=1:x; y<yRange[0]?y+=1:y>yRange[1]?y-=1:y; } return [x,y] }
이민희
function solution(keyinput, board) { const directions = { 'left': [-1, 0], 'right': [1, 0], 'up': [0, 1], 'down': [0, -1] } // 현재 위치 const pos = [0, 0]; // 범위 (최대/최소 X, Y 값) const borderX = Math.floor(board[0] / 2); const borderY = Math.floor(board[1] / 2); keyinput.forEach((key) => { const newX = pos[0] + directions[key][0]; const newY = pos[1] + directions[key][1]; if (newX <= borderX && newX >= -borderX && newY <= borderY && newY >= -borderY) { pos[0] = newX; pos[1] = newY; } }) return pos; }
박건우
function solution(keyinput, board) { const answer = [0, 0]; const direction = { up : [0, 1], down : [0, -1], left : [-1, 0], right : [1, 0], }; const [maxWidth, maxHeight] = board.map(v => Math.floor(v / 2)); //가로크기가 9면 -4~4 까지만 이동 가능 let [x, y] = [0, 0]; //시작 위치 for(let key of keyinput){ const [dx, dy] = direction[key]; const [nx, ny] = [x + dx, y + dy]; // 이동할 위치가 범위 안인지 확인 if(nx <= maxWidth && nx >= -maxWidth && ny <= maxHeight && ny >= -maxHeight){ [x, y] = [nx, ny]; } } return [x, y]; } // 방향이나 [x, y] 좌표 순서나 평소 문제풀때 쓰던방식이랑 달라서 좀 헤맸습니다.
박주연
function solution(keyinput, board) { const result = [0,0] const max = [Math.floor(board[0]/2),Math.floor(board[1]/2)]; for (let i of keyinput){ switch(i){ case "left": result[0] > -max[0] ? result[0]-- : null; break; case "right": result[0] < max[0] ? result[0]++ : null; break; case "up": result[1] < max[1] ? result[1]++ : null; break; case "down": result[1] > -max[1] ? result[1]-- : null; break; } } return result; }