c++:
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main(){
string s;
vector<string>temp;
while(cin >> s){
int sublen = 0;
for(int i = 0; i < s.size(); i++){
sublen++;
if(s[i] == ';'){
sublen -= 1;
temp.push_back(s.substr(i-sublen,sublen));
sublen = 0;
}
}
int num = 0;
int x = 0, y = 0;
for(int i = 0; i < temp.size(); i++){
if(temp[i].size() == 3 && isdigit(temp[i][1]) && isdigit(temp[i][2])){
num = (temp[i][1]-'0')*10 + (temp[i][2]-'0');
}else if(temp[i].size() == 2 && isdigit(temp[i][1])){
num = temp[i][1]-'0';
}else{
num = 0;
}
switch(temp[i][0]){
case 'A':x -= num;
break;
case 'D':x += num;
break;
case 'W':y += num;
break;
case 'S':y -= num;
}
}
cout << x << ',' << y<<endl;
return 0;
}
}
python:
s = input()
subs = s.split(';')
x = 0
y = 0
for c in subs:
if not 2<=len(c)<=3:
continue
try:
direction = c[0]
step = int(c[1:])
if direction in ['A','W','S','D']:
if direction == 'A':
x -= step
elif direction == 'D':
x += step
elif direction == 'S':
y -= step
elif direction == 'W':
y += step
except:
continue
print(str(x)+','+str(y))
|