题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;
void async function () {
// Write your code here
while(line = await readline()){
let arr = line.split(';');
let x = 0;
let y = 0;
for(let i = 0; i < arr.length; i++) {
// 判断有效值
if(isValidVal(arr[i])) {
// 匹配该项值,match[1] 是方向,match[2] 是步数
let match = arr[i].match(/(^[ADWS])(\d+$)/);
switch(match[1]){
case "D": x += parseInt(match[2]);break;
case "A": x -= parseInt(match[2]);break;
case "W": y += parseInt(match[2]);break;
case "S": y -= parseInt(match[2]);break;
}
}
}
console.log(`${x},${y}`);
}
}()
var isValidVal = function(val) {
return /^[ADWS]\d+$/.test(val);
}
var moveTo = function() {}