Book a free demo

Confident · ages 13+ · lists · permutations · while loop · maths

Simulate a 2×2 Rubik's Cube move in Python.

Represent a 2×2 cube as 24 stickers, apply the moves R and U as rearrangements, and count how many times R U repeats before the cube is solved again.

The program

Press Run to see what it prints. Change anything you like, then run it again.

# Stickers are numbered 0 to 23 (6 faces of 4). A move says where each sticker goes.
R = [0, 22, 2, 20, 5, 7, 4, 6, 8, 1, 10, 3, 12, 9, 14, 11, 16, 17, 18, 19, 15, 21, 13, 23]
U = [1, 3, 0, 2, 8, 9, 6, 7, 16, 17, 10, 11, 12, 13, 14, 15, 20, 21, 18, 19, 4, 5, 22, 23]

def apply(state, move):
    new = list(state)
    for i in range(24):
        new[move[i]] = state[i]
    return new

solved = list(range(24))
state = solved
repeats = 0
while True:
    state = apply(apply(state, R), U)
    repeats = repeats + 1
    if state == solved:
        break
print("R U repeats", repeats, "times on a 2x2 before the cube is solved again")
R U repeats 15 times on a 2x2 before the cube is solved again

How it works

  1. The cube has 24 stickers. A move is a list that says which place each sticker goes to. Doing a move means moving every sticker to its new place.
  2. apply makes a new list and puts each sticker where the move sends it. Doing R and then U is two calls to apply.
  3. The loop repeats R U until every sticker is back where it began. The count is the order of the move. See the maths of the cube for the 3×3 version.
See the Python path →

Try this

  • Count the repeats of R alone. What do you expect?
  • Try three moves in a row, such as R, U and R again.
  • Print the state after one R U to see the rearrangement.

Stuck on an error? Read how to debug Python.

Similar ideas

More programs

All programs →

FAQ

Simulate a 2×2 Rubik's Cube move: questions

What does the simulate a 2×2 rubik's cube move program do?

Represent a 2×2 cube as 24 stickers, apply the moves R and U as rearrangements, and count how many times R U repeats before the cube is solved again. It prints: R U repeats 15 times on a 2x2 before the cube is solved again

Which Python ideas does it use?

lists, permutations, while loop, maths. Confident level, suitable from about age 13.

How do I run it?

Press Run on this page. To run it on your own computer, install Python 3 and save the code in a file ending in .py. It prints the same thing.

What should I try next?

Count the repeats of R alone. What do you expect? Try three moves in a row, such as R, U and R again. Print the state after one R U to see the rearrangement.

Start a conversation

Want a teacher to guide the learning?

Tell us the learner's age and what they can already do, and we will suggest a starting point. A parent or guardian can make the enquiry.

  • Tell us the learner’s age or grade and goals
  • We’ll suggest a suitable class or learning path
  • We’ll confirm format, timing and fees with a parent or guardian

For a child’s class, please have a parent or guardian send the enquiry. Avoid sharing passwords or sensitive personal details.

We’ll use your details only to respond to this request.

Play it, don't just read it

Write some code right now

Free games that need no sign-up. Pick one and play for five minutes.

All free games →
Chat on WhatsApp
Esc

Try