Cody

Problem 892. Solve Rubik's Cube - Up to Two Face Moves

Solution 132445

Submitted on 29 Aug 2012 by James
  • Size: 64
  • This is the leading solution.
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

This solution is outdated. To rescore this solution, sign in.

Test Suite

Test Status Code Input and Output
1   Pass
%% % Load function rubik_rot.m tic urlwrite('http://tinyurl.com/matlab-rubik-rot','rubik_rot.m') ; rehash path toc

{Warning: Function /opt/mlsedu/mdcsserver/latest/m/web_common/shadow/license.m has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.} {> In verifyCode>evaluateCode at 189 In verifyCode at 40 In fevalJSON at 14} {Warning: Function /opt/mlsedu/mdcsserver/latest/m/web_common/shadow/graphicsAndGuis/uicontrol.m has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.} {> In verifyCode>evaluateCode at 189 In verifyCode at 40 In fevalJSON at 14} {Warning: Function /opt/mlsedu/mdcsserver/latest/m/common/shadow/home.m has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.} {> In verifyCode>evaluateCode at 189 In verifyCode at 40 In fevalJSON at 14} {Warning: Function /opt/mlsedu/mdcsserver/latest/m/common/shadow/keyboard.m has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.} {> In verifyCode>evaluateCode at 189 In verifyCode at 40 In fevalJSON at 14} {Warning: Function /opt/mlsedu/mdcsserver/latest/m/common/shadow/more.m has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.} {> In verifyCode>evaluateCode at 189 In verifyCode at 40 In fevalJSON at 14} {Warning: Function /opt/mlsedu/mdcsserver/latest/m/common/shadow/pause.m has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.} {> In verifyCode>evaluateCode at 189 In verifyCode at 40 In fevalJSON at 14} Elapsed time is 1.101123 seconds.

2   Pass
%% % For mov=1 solution is 7. Need U' to solve U in 1 move % mov=1; % U answer should be 7 % Solved Cube Vector r=[0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5]; r1=rubik_rot(1,r); % Create Challenge Cube mov_vec=rubik_solve(r1); for i=1:length(mov_vec) % Perform moves to see if it solves r1=rubik_rot(mov_vec(i),r1); end assert(isequal(length(mov_vec),1), [sprintf('Exp_moves=1 Exp Mov=7 moves=[') sprintf('%i ',mov_vec(:)) sprintf(']\n')]) assert(isequal(r1,r),sprintf('Exp Mov=7 moves=[%i %i]\n',mov_vec(:)))

3   Pass
%% % For mov=14 the solution is 14. Need F2 to solve F2 in 1 move % Solved Cube Vector r=[0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5]; r1=rubik_rot(14,r); % Create Challenge Cube mov_vec=rubik_solve(r1); for i=1:length(mov_vec) % Perform moves to see if it solves r1=rubik_rot(mov_vec(i),r1); end assert(isequal(length(mov_vec),1), [sprintf('Exp_moves=1 Exp Mov=14 moves=[') sprintf('%i ',mov_vec(:)) sprintf(']\n')]) assert(isequal(r1,r),sprintf('Exp Mov=14 move=[%i]\n',mov_vec(:)))

4   Pass
%% % Starting Cube Vector r=[0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5]; r1=rubik_rot(2,r); % Create Challenge Cube, First Twist r1=rubik_rot(9,r1); % Create Challenge Cube, Second Twist mov_vec=rubik_solve(r1); for i=1:length(mov_vec) % Perform moves to see if it solves r1=rubik_rot(mov_vec(i),r1); end assert(isequal(length(mov_vec),2), [sprintf('Exp_moves=2 Exp Mov=[3 8] moves=[') sprintf('%i ',mov_vec(:)) sprintf(']\n')]) assert(isequal(r1,r),sprintf('Exp Mov=[3 8] moves=[%i %i]\n',mov_vec(:)))

5   Pass
%% % Anti-Hard code solution Test Case #1 cmov=[7:12 1:6 13:18]; % Complementary Move % Starting Cube Vector r=[0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5]; move_map=[ 2 3 4 5 6 8 9 10 11 12 14 15 16 17 18; 1 3 4 5 6 7 9 10 11 12 13 15 16 17 18; 1 2 4 5 6 7 8 10 11 12 13 14 16 17 18; 1 2 3 5 6 7 8 9 11 12 13 14 15 17 18; 1 2 3 4 6 7 8 9 10 12 13 14 15 16 18; 1 2 3 4 5 7 8 9 10 11 13 14 15 16 17 ; 2 3 4 5 6 8 9 10 11 12 14 15 16 17 18; 1 3 4 5 6 7 9 10 11 12 13 15 16 17 18; 1 2 4 5 6 7 8 10 11 12 13 14 16 17 18; 1 2 3 5 6 7 8 9 11 12 13 14 15 17 18; 1 2 3 4 6 7 8 9 10 12 13 14 15 16 18; 1 2 3 4 5 7 8 9 10 11 13 14 15 16 17 ; 2 3 4 5 6 8 9 10 11 12 14 15 16 17 18; 1 3 4 5 6 7 9 10 11 12 13 15 16 17 18; 1 2 4 5 6 7 8 10 11 12 13 14 16 17 18; 1 2 3 5 6 7 8 9 11 12 13 14 15 17 18; 1 2 3 4 6 7 8 9 10 12 13 14 15 16 18; 1 2 3 4 5 7 8 9 10 11 13 14 15 16 17 ]; movt1=randi(18); movt2=move_map(movt1,randi(15));% Avoid complementary moves r1=rubik_rot(movt1,r); % Create Challenge Cube: First Turn r1=rubik_rot(movt2,r1); % Create Challenge Cube: Second Turn mov_vec=rubik_solve(r1); for i=1:length(mov_vec) % Perform moves to see if it solves r1=rubik_rot(mov_vec(i),r1); end assert(isequal(length(mov_vec),2), [sprintf('Exp_moves=2 Exp Mov=[%i %i] moves=[',cmov(movt2),cmov(movt1)) sprintf('%i ',mov_vec(:)) sprintf(']\n')]) assert(isequal(r1,r),sprintf('Exp Mov=[%i %i] moves=[%i %i]\n',cmov(movt2),cmov(movt1),mov_vec(:)))

6   Pass
%% % Anti-Hard code solution Test Case #2 cmov=[7:12 1:6 13:18]; % Complementary Move % Starting Cube Vector r=[0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5]; move_map=[ 2 3 4 5 6 8 9 10 11 12 14 15 16 17 18; 1 3 4 5 6 7 9 10 11 12 13 15 16 17 18; 1 2 4 5 6 7 8 10 11 12 13 14 16 17 18; 1 2 3 5 6 7 8 9 11 12 13 14 15 17 18; 1 2 3 4 6 7 8 9 10 12 13 14 15 16 18; 1 2 3 4 5 7 8 9 10 11 13 14 15 16 17 ; 2 3 4 5 6 8 9 10 11 12 14 15 16 17 18; 1 3 4 5 6 7 9 10 11 12 13 15 16 17 18; 1 2 4 5 6 7 8 10 11 12 13 14 16 17 18; 1 2 3 5 6 7 8 9 11 12 13 14 15 17 18; 1 2 3 4 6 7 8 9 10 12 13 14 15 16 18; 1 2 3 4 5 7 8 9 10 11 13 14 15 16 17 ; 2 3 4 5 6 8 9 10 11 12 14 15 16 17 18; 1 3 4 5 6 7 9 10 11 12 13 15 16 17 18; 1 2 4 5 6 7 8 10 11 12 13 14 16 17 18; 1 2 3 5 6 7 8 9 11 12 13 14 15 17 18; 1 2 3 4 6 7 8 9 10 12 13 14 15 16 18; 1 2 3 4 5 7 8 9 10 11 13 14 15 16 17 ]; mov2t1=randi(18); mov2t2=move_map(mov2t1,randi(15));% Avoid complementary moves r1=rubik_rot(mov2t1,r); % Create Challenge Cube: First Turn r1=rubik_rot(mov2t2,r1); % Create Challenge Cube: Second Turn mov_vec=rubik_solve(r1); for i=1:length(mov_vec) % Perform moves to see if it solves r1=rubik_rot(mov_vec(i),r1); end assert(isequal(length(mov_vec),2), [sprintf('Exp_moves=2 Exp Mov=[%i %i] moves=[',cmov(mov2t2),cmov(mov2t1)) sprintf('%i ',mov_vec(:)) sprintf(']\n')]) assert(isequal(r1,r),sprintf('Exp Mov=[%i %i] moves=[%i %i]\n',cmov(mov2t2),cmov(mov2t1),mov_vec(:)))

Suggested Problems

More from this Author246

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!