-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoboScript3ImplementTheRS2Specification.java
More file actions
207 lines (163 loc) · 6.43 KB
/
Copy pathRoboScript3ImplementTheRS2Specification.java
File metadata and controls
207 lines (163 loc) · 6.43 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package kyu4;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
public class RoboScript3ImplementTheRS2Specification {
private enum Instruction {
FORWARD('F'),
ROTATE_LEFT('L'),
ROTATE_RIGHT('R');
private final char value;
Instruction(char value) {
this.value = value;
}
public static Instruction findByValue(Character c) {
return Arrays.stream(values())
.filter(v -> v.value == c)
.findFirst()
.orElse(null);
}
}
private record Command(List<Command> commands, Instruction instruction, int times) {
}
private record Position(int i, int j) {
}
private static class Cursor {
int i = 0;
int j = 0;
Direction direction = Direction.RIGHT;
public Cursor() {
}
public Position getPosition() {
return new Position(i, j);
}
public void moveForward() {
if (direction.equals(Direction.LEFT)) {
j = j - 1;
} else if (direction.equals(Direction.RIGHT)) {
j = j + 1;
} else if (direction.equals(Direction.UP)) {
i = i - 1;
} else {
i = i + 1;
}
}
public void rotateRight() {
if (direction.equals(Direction.LEFT)) {
direction = Direction.UP;
} else if (direction.equals(Direction.RIGHT)) {
direction = Direction.DOWN;
} else if (direction.equals(Direction.UP)) {
direction = Direction.RIGHT;
} else {
direction = Direction.LEFT;
}
}
public void rotateLeft() {
if (direction.equals(Direction.LEFT)) {
direction = Direction.DOWN;
} else if (direction.equals(Direction.RIGHT)) {
direction = Direction.UP;
} else if (direction.equals(Direction.UP)) {
direction = Direction.LEFT;
} else {
direction = Direction.RIGHT;
}
}
}
private enum Direction {
LEFT,
RIGHT,
UP,
DOWN
}
public static String execute(String code) {
final Cursor cursor = new Cursor();
final ArrayList<Position> positions = new ArrayList<>();
positions.add(cursor.getPosition());
int minI = 0;
int minJ = 0;
int maxI = 0;
int maxJ = 0;
final ArrayList<Command> commands = parseCommands(code);
getPositions(commands, positions, cursor);
for (Position position : positions) {
if (position.i < minI) minI = position.i;
if (position.i > maxI) maxI = position.i;
if (position.j < minJ) minJ = position.j;
if (position.j > maxJ) maxJ = position.j;
}
Character[][] board = new Character[maxI - minI + 1][maxJ - minJ + 1];
for (Position position : positions) {
board[position.i + Math.abs(minI)][position.j + Math.abs(minJ)] = '*';
}
return Arrays.stream(board).map(row -> Arrays.stream(row)
.map(c -> c == null ? " " : "*")
.collect(Collectors.joining()))
.collect(Collectors.joining("\r\n"));
}
private static ArrayList<Command> parseCommands(String code) {
ArrayList<Command> commands = new ArrayList<>();
parseCommandsAux(code, commands, 0);
return commands;
}
private static int parseCommandsAux(String code, ArrayList<Command> commands, int startIndex) {
Character previousChar = null;
String previousnumber = null;
ArrayList<Command> previousCommands = new ArrayList<>();
for (int i = startIndex; i < code.length(); i++) {
char c = code.charAt(i);
if (c == 'F' || c == 'R' || c == 'L') {
addToCommands(commands, previousCommands, previousChar, previousnumber);
previousChar = c;
previousnumber = null;
previousCommands = new ArrayList<>();
} else if (Character.isDigit(c)) {
previousnumber = previousnumber == null ? String.valueOf(c) : previousnumber + c;
} else if (c == '(') {
addToCommands(commands, previousCommands, previousChar, previousnumber);
previousChar = null;
previousnumber = null;
previousCommands = new ArrayList<>();
i = parseCommandsAux(code, previousCommands, i + 1);
} else if (c == ')') {
addToCommands(commands, previousCommands, previousChar, previousnumber);
return i;
}
}
addToCommands(commands, previousCommands, previousChar, previousnumber);
return code.length();
}
private static void addToCommands(List<Command> commands, List<Command> previousCommands, Character character, String times) {
int number = times == null ? 1 : Integer.parseInt(times);
if (character != null) {
commands.add(new Command(Collections.emptyList(), Instruction.findByValue(character), number));
} else if (!previousCommands.isEmpty()) {
commands.add(new Command(previousCommands, null, number));
}
}
private static void getPositions(List<Command> commands, List<Position> positions, Cursor cursor) {
for (Command command : commands) {
if (!command.commands.isEmpty()) {
for (int i = 0; i < command.times; i++) {
getPositions(command.commands, positions, cursor);
}
} else if (command.instruction.equals(Instruction.FORWARD)) {
for (int i = 0; i < command.times; i++) {
cursor.moveForward();
positions.add(cursor.getPosition());
}
} else if (command.instruction.equals(Instruction.ROTATE_LEFT)) {
for (int i = 0; i < command.times; i++) {
cursor.rotateLeft();
}
} else if (command.instruction.equals(Instruction.ROTATE_RIGHT)) {
for (int i = 0; i < command.times; i++) {
cursor.rotateRight();
}
}
}
}
}