|
| 1 | +const input = require('fs') |
| 2 | + .readFileSync(process.platform === 'linux' ? '/dev/stdin' : './input.txt') |
| 3 | + .toString() |
| 4 | + .trim() |
| 5 | + .split('\n'); |
| 6 | + |
| 7 | +function solution(input) { |
| 8 | + const n = Number(input[1]); |
| 9 | + const inputs = input.slice(2); |
| 10 | + |
| 11 | + let left = input[0].split(''); |
| 12 | + let right = []; |
| 13 | + |
| 14 | + for (let i = 0; i < n; i++) { |
| 15 | + if (inputs[i] === 'L') { |
| 16 | + if (left.length !== 0) { |
| 17 | + right.push(left.pop()); |
| 18 | + } |
| 19 | + // console.log('L : 커서 왼쪽이동', left, right); |
| 20 | + } else if (inputs[i] === 'D') { |
| 21 | + if (right.length !== 0) { |
| 22 | + left.push(right.pop()); |
| 23 | + } |
| 24 | + // console.log('D : 커서 오른쪽이동', left, right); |
| 25 | + } else if (inputs[i] === 'B') { |
| 26 | + if (left.length !== 0) { |
| 27 | + left.pop(); |
| 28 | + } |
| 29 | + // console.log('B : 왼쪽 삭제', left, right); |
| 30 | + } else { |
| 31 | + left.push(inputs[i].split(' ')[1]); |
| 32 | + // console.log('P : 왼쪽 추가', left, right); |
| 33 | + } |
| 34 | + } |
| 35 | + // console.log(left, right); |
| 36 | + |
| 37 | + return left.join('').trim() + right.reverse().join('').trim(); |
| 38 | +} |
| 39 | + |
| 40 | +console.log(solution(input)); |
0 commit comments