Simulating Dice Rotations Using Character Commands in Multiple Languages
Problem Outline
A standard cube-shaped die starts with the initial configuration left=1, right=2, front=3, back=4, top=5, bottom=6, represented as the string 123456. Given a sequence of operations—each being a single character—the task is to output the final configuration after applying all moves.
Allowed operations:
L: roll left onceR: roll right onceF: roll forward onceB: roll backward onceA: rotate 90° anticlockwise (as viewed from above)C: rotate 90° clockwise (as viewed from above)
The input is a continuous string of these letters (maximum length 50), and the output must be the cnocatenated six digits reflecting the final state.
Internal Representation
We keep six integer slots in a fixed order: [left, right, front, back, top, bottom]. When a move occurs, the values shift among the slots according to predetermined permutation rules.
Movement mappings (positional indices 0-based):
L (roll left)
New arrangement:
left = old front
front = old bottom
bottom = old back
back = old left
R (roll right)
New arrangement:
left = old back
back = old bottom
bottom = old front
front = old left
F (roll forward)
New arrangement:
left = old top
top = old back
back = old bottom
bottom = old left
B (roll backward)
New arrangement:
left = old bottom
bottom = old back
back = old top
top = old left
A (anticlockwise spin)
New arrangement:
left = old front
front = old right
right = old back
back = old left
C (clockwise spin)
New arrangement:
left = old back
back = old right
right = old front
front = old left
All other slots remain unchanged during a given operation.
Implementation Examples
Each sample below reads a single line from standard input, applies the string of commands, and prints the resulting six-digit state.
Java
import java.util.Scanner;
class DieSimulator {
private int[] state = {1, 2, 3, 4, 5, 6}; // left, right, front, back, top, bottom
private int l() { return state[0]; }
private int r() { return state[1]; }
private int f() { return state[2]; }
private int b() { return state[3]; }
private int t() { return state[4]; }
private int d() { return state[5]; }
private void setL(int v) { state[0] = v; }
private void setR(int v) { state[1] = v; }
private void setF(int v) { state[2] = v; }
private void setB(int v) { state[3] = v; }
private void setT(int v) { state[4] = v; }
private void setD(int v) { state[5] = v; }
public void rollLeft() {
int oldL = l(), oldF = f(), oldD = d(), oldB = b();
setL(oldF); setF(oldD); setD(oldB); setB(oldL);
}
public void rollRight() {
int oldL = l(), oldF = f(), oldD = d(), oldB = b();
setL(oldB); setF(oldL); setD(oldF); setB(oldD);
}
public void rollForward() {
int oldL = l(), oldT = t(), oldB = b(), oldD = d();
setL(oldT); setT(oldB); setB(oldD); setD(oldL);
}
public void rollBackward() {
int oldL = l(), oldT = t(), oldB = b(), oldD = d();
setL(oldD); setD(oldB); setB(oldT); setT(oldL);
}
public void spinAnticlockwise() {
int oldL = l(), oldF = f(), oldR = r(), oldB = b();
setL(oldF); setF(oldR); setR(oldB); setB(oldL);
}
public void spinClockwise() {
int oldL = l(), oldF = f(), oldR = r(), oldB = b();
setL(oldB); setB(oldR); setR(oldF); setF(oldL);
}
public String getState() {
StringBuilder sb = new StringBuilder();
for (int face : state) sb.append(face);
return sb.toString();
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String commands = sc.nextLine().trim();
DieSimulator die = new DieSimulator();
for (char ch : commands.toCharArray()) {
switch (ch) {
case 'L': die.rollLeft(); break;
case 'R': die.rollRight(); break;
case 'F': die.rollForward(); break;
case 'B': die.rollBackward(); break;
case 'A': die.spinAnticlockwise(); break;
case 'C': die.spinClockwise(); break;
default: // ignore invalid characters
}
}
System.out.println(die.getState());
sc.close();
}
}
Python
import sys
class Cube:
__slots__ = ('sides',)
def __init__(self):
# left, right, front, back, top, bottom
self.sides = [1, 2, 3, 4, 5, 6]
def execute(self, cmd):
if cmd == 'L':
l, f, d, b = self.sides[0], self.sides[2], self.sides[5], self.sides[3]
self.sides[0], self.sides[2], self.sides[5], self.sides[3] = f, d, b, l
elif cmd == 'R':
l, f, d, b = self.sides[0], self.sides[2], self.sides[5], self.sides[3]
self.sides[0], self.sides[2], self.sides[5], self.sides[3] = b, l, f, d
elif cmd == 'F':
l, t, b, d = self.sides[0], self.sides[4], self.sides[3], self.sides[5]
self.sides[0], self.sides[4], self.sides[3], self.sides[5] = t, b, d, l
elif cmd == 'B':
l, t, b, d = self.sides[0], self.sides[4], self.sides[3], self.sides[5]
self.sides[0], self.sides[4], self.sides[3], self.sides[5] = d, l, t, b
elif cmd == 'A':
l, f, r, b = self.sides[0], self.sides[2], self.sides[1], self.sides[3]
self.sides[0], self.sides[2], self.sides[1], self.sides[3] = f, r, b, l
elif cmd == 'C':
l, f, r, b = self.sides[0], self.sides[2], self.sides[1], self.sides[3]
self.sides[0], self.sides[2], self.sides[1], self.sides[3] = b, l, f, r
def __str__(self):
return ''.join(map(str, self.sides))
def main():
sequence = sys.stdin.readline().strip()
cube = Cube()
for ch in sequence:
cube.execute(ch)
print(cube)
if __name__ == '__main__':
main()
JavaScript (Node.js)
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.on('line', (input) => {
const ops = input.trim().split('');
const die = {
left: 1, right: 2, front: 3, back: 4, top: 5, bottom: 6
};
const rollLeft = () => {
const { left, front, bottom, back } = die;
die.left = front; die.front = bottom; die.bottom = back; die.back = left;
};
const rollRight = () => {
const { left, front, bottom, back } = die;
die.left = back; die.front = left; die.bottom = front; die.back = bottom;
};
const rollForward = () => {
const { left, top, back, bottom } = die;
die.left = top; die.top = back; die.back = bottom; die.bottom = left;
};
const rollBackward = () => {
const { left, top, back, bottom } = die;
die.left = bottom; die.bottom = back; die.back = top; die.top = left;
};
const spinAnticlockwise = () => {
const { left, front, right, back } = die;
die.left = front; die.front = right; die.right = back; die.back = left;
};
const spinClockwise = () => {
const { left, front, right, back } = die;
die.left = back; die.back = right; die.right = front; die.front = left;
};
const actions = {
L: rollLeft,
R: rollRight,
F: rollForward,
B: rollBackward,
A: spinAnticlockwise,
C: spinClockwise
};
for (let op of ops) {
if (actions[op]) actions[op]();
}
console.log(`${die.left}${die.right}${die.front}${die.back}${die.top}${die.bottom}`);
rl.close();
});
Notes
- Input should be read as a single string; spaces between characters are not required.
- Only the uppercase letters
L,R,F,B,A,Care valid. Any other characters can be safely skipped. - The initial orientation is always
123456. The output must be exactly six digits with no separators.