-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoboScript2ImplementTheRS1Specification.java
More file actions
140 lines (112 loc) · 4.26 KB
/
Copy pathRoboScript2ImplementTheRS1Specification.java
File metadata and controls
140 lines (112 loc) · 4.26 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
package kyu5;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class RoboScript2ImplementTheRS1Specification {
private static final Pattern pattern = Pattern.compile("[LFR]\\d*");
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(Instruction instruction, int times) {
}
private record Position(int i, int j) {
}
private enum Direction {
LEFT,
RIGHT,
UP,
DOWN
}
public static String execute(String code) {
final Matcher matcher = pattern.matcher(code);
Direction direction = Direction.RIGHT;
Position p = new Position(0, 0);
ArrayList<Position> positions = new ArrayList<>();
int minI = 0;
int minJ = 0;
int maxI = 0;
int maxJ = 0;
positions.add(p);
while (matcher.find()) {
final Command command = parseCommand(matcher.group());
if (command.instruction.equals(Instruction.FORWARD)) {
for (int i = 0; i < command.times; i++) {
p = moveForward(p, direction);
positions.add(p);
if (p.i < minI) minI = p.i;
if (p.i > maxI) maxI = p.i;
if (p.j < minJ) minJ = p.j;
if (p.j > maxJ) maxJ = p.j;
}
} else if (command.instruction.equals(Instruction.ROTATE_LEFT)) {
for (int i = 0; i < command.times; i++) {
direction = rotateLeft(direction);
}
} else if (command.instruction.equals(Instruction.ROTATE_RIGHT)) {
for (int i = 0; i < command.times; i++) {
direction = rotateRight(direction);
}
}
}
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 Command parseCommand(String command) {
Instruction instruction = Instruction.findByValue(command.charAt(0));
int times = command.substring(1).equals("") ? 1 : Integer.parseInt(command.substring(1));
return new Command(instruction, times);
}
private static Position moveForward(Position position, Direction direction) {
if (direction.equals(Direction.LEFT)) {
return new Position(position.i, position.j - 1);
} else if (direction.equals(Direction.RIGHT)) {
return new Position(position.i, position.j + 1);
} else if (direction.equals(Direction.UP)) {
return new Position(position.i - 1, position.j);
} else {
return new Position(position.i + 1, position.j);
}
}
private static Direction rotateRight(Direction direction) {
if (direction.equals(Direction.LEFT)) {
return Direction.UP;
} else if (direction.equals(Direction.RIGHT)) {
return Direction.DOWN;
} else if (direction.equals(Direction.UP)) {
return Direction.RIGHT;
} else {
return Direction.LEFT;
}
}
private static Direction rotateLeft(Direction direction) {
if (direction.equals(Direction.LEFT)) {
return Direction.DOWN;
} else if (direction.equals(Direction.RIGHT)) {
return Direction.UP;
} else if (direction.equals(Direction.UP)) {
return Direction.LEFT;
} else {
return Direction.RIGHT;
}
}
}