Arrays & Hashing · Pattern 19 of 26
Simulation: do exactly what the problem says
No trick, just careful bookkeeping. Represent the state cleanly (direction vectors, an index into the input), handle every edge case the statement mentions, and test on the examples.
Watch the lesson
Concepts first, then the problem and every approach, step by step.
Space to play or pause · ← → to jump a step · click or drag the bar to seek
When to use it
- A robot, a grid or a game that follows step-by-step rules
- Parse a string into a number, an IP address or code
- Format text into lines, words or a pattern
The template
// a robot on a grid: directions as vectors, turning changes the index
boolean isRobotBounded(String instructions) {
int[][] dirs = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; // north, east, south, west
int x = 0, y = 0, d = 0;
for (char c : instructions.toCharArray()) {
if (c == 'G') { x += dirs[d][0]; y += dirs[d][1]; }
else if (c == 'R') d = (d + 1) % 4;
else d = (d + 3) % 4; // L
}
return (x == 0 && y == 0) || d != 0; // back home, or facing a new way: it loops
} def is_robot_bounded(instructions):
dirs = [(0, 1), (1, 0), (0, -1), (-1, 0)] # north, east, south, west
x = y = d = 0
for c in instructions:
if c == 'G':
x, y = x + dirs[d][0], y + dirs[d][1]
elif c == 'R':
d = (d + 1) % 4
else:
d = (d + 3) % 4 # L
return (x, y) == (0, 0) or d != 0 # back home, or facing a new way: it loops // a robot on a grid: directions as vectors, turning changes the index
bool isRobotBounded(string instructions) {
int dirs[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; // north, east, south, west
int x = 0, y = 0, d = 0;
for (char c : instructions) {
if (c == 'G') { x += dirs[d][0]; y += dirs[d][1]; }
else if (c == 'R') d = (d + 1) % 4;
else d = (d + 3) % 4; // L
}
return (x == 0 && y == 0) || d != 0; // back home, or facing a new way: it loops
} // a robot on a grid: directions as vectors, turning changes the index
function isRobotBounded(instructions) {
const dirs = [[0, 1], [1, 0], [0, -1], [-1, 0]]; // north, east, south, west
let x = 0, y = 0, d = 0;
for (const c of instructions) {
if (c === 'G') { x += dirs[d][0]; y += dirs[d][1]; }
else if (c === 'R') d = (d + 1) % 4;
else d = (d + 3) % 4; // L
}
return (x === 0 && y === 0) || d !== 0; // back home, or facing a new way: it loops
} The variations
-
1Directions and grids
Keep the direction as an index into a vector array; turning is ± 1 mod 4. Repeat gravity or crushing until nothing changes.
Now you solve- Pour Water Medium Stuck? solution
- Candy Crush Medium Stuck? solution
-
2Parse carefully
Walk the string with an index: skip spaces, read the sign, read digits, stop early on overflow. Split on separators and validate each part.
Now you solve -
3Build the output line by line
Decide what goes on each line or row, then format it. Zigzag is "which row does character i go to"; text justification spreads spaces left first.
Now you solve -
4More practice
More problems that use this pattern. Try them once the variations above feel easy.
Problems
Mistakes to avoid
- Writing four copies of the same move code instead of one direction array.
- Skipping edge cases the statement lists: leading spaces, signs, overflow, empty lines.
- Not re-reading the statement after coding; simulations fail on details.
Lesson done
Next: The contribution technique →Mark it done once you can write the template from memory and you have solved the worked examples yourself.