-
Notifications
You must be signed in to change notification settings - Fork 1
/
Move Robot.txt
89 lines (83 loc) · 1.64 KB
/
Move Robot.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
int xGrid = input1;
int yGrid = input2;
String current[] = input3.split("-");
int x = Integer.parseInt(current[0]);
int y = Integer.parseInt(current[1]);
char D = current[2].charAt(0); //Direction robot is facing
String msg = input4; //contains series of M, L or R
String error="";
StringBuffer out = new StringBuffer();
for(int i=0; i<msg.length();i++){
if(msg.charAt(i) == 'M'){
if(D == 'N'){
y++;
}
else if(D == 'S'){
y--;
}
else if(D == 'E'){
x++;
}
else if(D == 'W'){
x--;
}
if(x>xGrid){
x--;
error = "ER";
break;
}
else if(x<0){
x++;
error = "ER";
break;
}
if(y>yGrid){
y--;
error = "ER";
break;
}
else if(y<0){
y++;
error = "ER";
break;
}
}
else if(msg.charAt(i) == 'L'){
switch (D){ //switch on current position
case 'N':
D = 'W';
break;
case 'S':
D = 'E';
break;
case 'E':
D = 'N';
break;
case 'W':
D = 'S';
break;
}
}
else if(msg.charAt(i) == 'R'){
switch (D){ //switch on current position
case 'N':
D = 'E';
break;
case 'S':
D = 'W';
break;
case 'E':
D = 'S';
break;
case 'W':
D = 'N';
break;
}
}
}
System.out.println(x + " " + y + " " + D);
out.append(Integer.toString(x) + "-" + Integer.toString(y) + "-" + Character.toString(D));
if(error.length()>0){
out.append("-" + error);
}
return out.toString();