Cody

# Problem 44376. The sliding puzzle: 3D

Solution 1399415

Submitted on 31 Dec 2017 by Thomas Steffen
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

### Test Suite

Test Status Code Input and Output
1   Pass
filetext = fileread('sliding3d.m'); assert(isempty(strfind(filetext,'eval'))) assert(isempty(strfind(filetext,'echo')))

2   Pass
p = [0;2;3;1;5;6;4;7;9;10;11;12;13;17;14;16;8;18;19;20;21;22;23;15;25;26;24]; m = sliding3d(p); for i = 1:numel(m) if round(m(i)) == m(i) && m(i) <= numel(p) && m(i) > 0 zero=find(p == 0); [a0 b0 c0] = ind2sub([3 3 3],zero); [a1 b1 c1] = ind2sub([3 3 3],m(i)); if abs(a1 - a0) + abs(b1 - b0) + abs(c1 - c0) == 1 p([m(i) zero]) = p([zero m(i)]); end end end assert(isequal(p,[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;0]))

ans = 19 20 21 22 25 24 26 23 0 ans = 19 20 21 22 23 24 25 26 0 ans = 26 m = Columns 1 through 29 2 3 6 9 18 27 26 25 22 13 4 5 14 23 24 27 26 25 22 19 10 13 22 23 24 27 18 9 8 Columns 30 through 58 7 4 1 10 11 12 15 18 27 26 25 22 13 4 5 14 23 24 27 26 23 20 11 10 13 22 23 24 27 Columns 59 through 87 26 23 20 11 10 13 22 23 24 27 18 9 8 5 2 11 12 15 18 27 26 23 14 5 6 15 24 27 24 Columns 88 through 116 21 12 11 14 23 24 27 24 21 12 11 14 23 24 27 18 9 6 3 12 15 18 27 26 25 22 13 14 15 Columns 117 through 145 24 27 26 25 22 13 14 15 24 27 18 9 8 7 4 13 14 15 18 27 26 17 8 9 18 27 26 23 14 Columns 146 through 174 17 26 27 18 9 8 5 14 15 18 27 26 25 16 17 14 23 24 27 26 25 16 17 14 23 24 27 18 9 Columns 175 through 203 8 7 16 17 18 27 24 15 14 23 24 27 26 17 14 23 24 27 18 9 8 17 18 27 24 23 22 25 16 Columns 204 through 232 7 8 17 18 9 8 7 16 25 22 23 24 27 18 17 16 13 10 19 20 21 24 27 26 25 22 19 20 21 Columns 233 through 261 24 27 26 25 22 19 20 21 24 27 26 25 22 19 20 21 24 27 26 25 22 19 20 21 24 27 26 25 22 Columns 262 through 290 19 20 21 24 27 26 25 22 19 20 21 24 27 18 17 14 11 20 21 24 27 26 25 22 19 20 21 24 27 Columns 291 through 319 18 15 12 21 24 27 26 25 22 19 20 21 24 27 26 25 22 19 20 21 24 27 26 25 22 19 20 21 24 Columns 320 through 348 27 26 25 22 19 20 21 24 27 18 17 16 13 22 23 24 27 18 17 14 23 24 27 26 23 20 21 24 27 Columns 349 through 377 26 23 20 21 24 27 18 15 24 27 26 25 22 19 20 21 24 27 26 25 22 19 20 21 24 27 26 25 22 Columns 378 through 406 19 20 21 24 27 26 25 22 19 20 21 24 27 18 17 16 25 26 27 26 25 22 19 20 21 24 27 26 25 Columns 407 through 435 22 19 20 21 24 27 26 25 22 19 20 21 24 27 26 25 22 19 20 21 24 27 18 17 26 27 26 25 22 Columns 436 through 464 19 20 21 20 19 20 21 24 27 26 25 22 19 20 21 20 19 20 21 24 27 26 25 22 19 20 21 20 19 Columns 465 through 493 20 21 24 27 26 25 22 19 20 21 20 19 20 21 24 27 26 25 22 19 20 21 20 19 20 21 24 27 26 Columns 494 through 522 25 22 19 20 21 20 19 20 21 24 27 24 23 22 25 16 17 26 27 18 17 16 25 22 23 24 27 26 25 Columns 523 through 551 22 19 20 21 20 19 20 21 24 27 26 25 22 19 20 21 20 19 20 21 24 27 26 25 22 19 20 21 20 Columns 552 through 580 19 20 21 24 27 26 23 20 21 20 21 24 27 26 23 20 21 20 21 24 27 18 17 14 11 20 21 24 23 Columns 581 through 609 24 23 20 11 14 17 18 27 26 25 22 23 24 23 22 23 24 27 26 25 22 23 24 23 22 23 24 27 26 Columns 610 through 634 25 22 23 24 23 22 23 24 27 18 17 16 13 22 23 26 25 26 25 22 13 16 17 18 27

3   Pass
p = [1;0;2;4;8;5;7;9;18;10;21;12;11;14;15;16;17;6;13;3;19;20;22;24;25;23;26]; m = sliding3d(p); for i = 1:numel(m) if round(m(i)) == m(i) && m(i) <= numel(p) && m(i) > 0 zero=find(p == 0); [a0 b0 c0] = ind2sub([3 3 3],zero); [a1 b1 c1] = ind2sub([3 3 3],m(i)); if abs(a1 - a0) + abs(b1 - b0) + abs(c1 - c0) == 1 p([m(i) zero]) = p([zero m(i)]); end end end assert(isequal(p,[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;0]))

ans = 19 20 21 22 24 25 23 26 0 ans = 19 20 21 22 23 24 25 26 0 ans = 26 m = Columns 1 through 29 3 6 9 18 27 24 21 12 11 20 21 24 27 24 21 12 11 20 21 24 27 18 9 6 3 12 15 18 27 Columns 30 through 58 24 15 6 9 18 27 26 23 14 15 24 27 18 9 8 5 14 15 18 27 24 15 14 23 24 27 18 9 6 Columns 59 through 87 15 18 27 24 15 18 27 26 25 22 19 20 21 24 27 26 25 22 19 20 21 24 27 26 25 22 19 20 21 Columns 88 through 116 24 27 26 25 22 19 20 21 24 27 26 25 22 19 20 21 24 27 26 25 22 19 20 21 24 27 18 17 26 Columns 117 through 145 27 24 23 22 25 16 7 8 17 18 9 8 7 16 25 22 23 24 27 26 25 22 13 14 23 24 27 26 25 Columns 146 through 174 22 19 20 21 24 27 26 25 22 19 20 21 24 27 26 25 22 19 20 21 24 27 26 25 22 19 20 21 24 Columns 175 through 203 27 26 25 22 19 20 21 24 27 18 17 14 11 20 21 24 27 26 23 14 15 24 27 26 23 20 21 24 27 Columns 204 through 232 26 23 20 21 24 27 26 23 20 21 24 27 18 15 12 21 24 27 26 25 22 19 20 21 24 27 26 25 22 Columns 233 through 261 19 20 21 24 27 26 25 22 19 20 21 24 27 26 25 22 19 20 21 24 27 26 25 22 19 20 21 24 27 Columns 262 through 290 18 17 16 13 22 23 24 27 26 25 16 17 26 27 26 25 22 19 20 23 26 27 18 17 14 23 24 27 18 Columns 291 through 319 17 26 27 18 17 26 27 26 23 24 23 24 27 24 23 22 25 16 17 26 27 18 17 16 25 22 23 24 27 Columns 320 through 348 26 25 22 19 20 21 20 19 20 21 24 27 26 23 20 21 20 21 24 27 18 17 14 11 20 21 24 23 24 Columns 349 through 377 23 20 11 14 17 18 27 26 23 24 23 24 27 18 17 16 13 22 23 26 25 26 25 22 13 16 17 18 27 Columns 378 through 389 26 23 24 23 24 27 26 23 24 23 24 27

4   Pass
p = [1;4;3;19;10;6;7;2;9;20;11;0;16;14;12;5;13;18;22;23;21;17;15;24;25;8;26]; m = sliding3d(p); for i = 1:numel(m) if round(m(i)) == m(i) && m(i) <= numel(p) && m(i) > 0 zero=find(p == 0); [a0 b0 c0] = ind2sub([3 3 3],zero); [a1 b1 c1] = ind2sub([3 3 3],m(i)); if abs(a1 - a0) + abs(b1 - b0) + abs(c1 - c0) == 1 p([m(i) zero]) = p([zero m(i)]); end end end assert(isequal(p,[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;0]))

ans = 19 20 21 22 24 26 25 23 0 ans = 19 20 21 22 23 24 25 26 0 ans = 26 m = Columns 1 through 29 15 18 27 26 17 8 9 18 27 26 23 20 11 14 17 26 27 26 23 20 11 14 17 26 27 18 9 8 5 Columns 30 through 58 2 11 12 15 18 27 26 23 14 5 6 15 24 27 26 25 22 13 14 23 24 27 18 9 8 7 4 13 14 Columns 59 through 87 15 18 27 26 23 14 13 16 25 26 27 26 23 14 13 16 25 26 27 18 9 8 5 14 15 18 27 26 17 Columns 88 through 116 8 9 18 27 24 15 14 17 26 27 24 15 14 17 26 27 18 9 6 15 18 27 26 17 8 9 18 27 26 Columns 117 through 145 25 16 17 26 27 18 9 8 7 16 17 18 27 26 17 18 15 24 27 26 17 18 15 24 27 18 9 8 17 Columns 146 through 174 18 27 18 17 26 27 18 17 26 27 24 23 22 25 16 7 8 17 18 9 8 7 16 25 22 23 24 27 26 Columns 175 through 203 17 18 27 26 25 22 19 20 21 24 27 26 25 22 19 20 21 24 27 26 25 22 19 20 21 24 27 26 25 Columns 204 through 232 22 19 20 21 24 27 18 17 16 13 10 19 20 21 24 27 24 15 18 27 26 25 22 19 20 21 24 27 26 Columns 233 through 261 25 22 19 20 21 24 27 18 17 14 11 20 21 24 27 26 23 14 15 24 27 26 23 20 21 24 27 26 23 Columns 262 through 290 20 21 24 27 26 23 20 21 24 27 18 15 12 21 24 27 26 25 22 19 20 21 24 27 26 25 22 19 20 Columns 291 through 319 21 24 27 26 25 22 19 20 21 24 27 26 25 22 19 20 21 24 27 26 25 22 19 20 21 24 27 26 25 Columns 320 through 348 22 19 20 21 24 27 18 17 16 13 22 23 24 27 18 17 14 23 24 27 26 25 22 19 20 21 24 27 26 Columns 349 through 377 25 22 19 20 21 24 27 26 25 22 19 20 21 24 27 26 25 22 19 20 21 24 27 26 25 22 19 20 21 Columns 378 through 406 24 27 26 25 22 19 20 21 24 27 18 15 24 27 26 25 22 19 20 21 24 27 26 25 22 19 20 21 24 Columns 407 through 435 27 26 25 22 19 20 21 24 27 18 17 16 25 26 27 26 25 22 19 20 21 24 27 26 25 22 19 20 21 Columns 436 through 464 24 27 26 25 22 19 20 21 24 27 18 17 26 27 26 25 22 19 20 21 20 19 20 21 24 27 26 25 22 Columns 465 through 493 19 20 21 20 19 20 21 24 27 26 25 22 19 20 21 20 19 20 21 24 27 26 25 22 19 20 21 20 19 Columns 494 through 522 20 21 24 27 24 23 22 25 16 17 26 27 18 17 16 25 22 23 24 27 26 23 20 21 20 21 24 27 26 Columns 523 through 541 23 20 21 20 21 24 27 26 23 24 23 24 27 26 23 24 23 24 27

5   Pass
p = [1;5;3;10;7;0;13;9;24;22;2;15;16;11;17;12;8;6;19;20;21;26;14;23;4;25;18]; m = sliding3d(p); for i = 1:numel(m) if round(m(i)) == m(i) && m(i) <= numel(p) && m(i) > 0 zero=find(p == 0); [a0 b0 c0] = ind2sub([3 3 3],zero); [a1 b1 c1] = ind2sub([3 3 3],m(i)); if abs(a1 - a0) + abs(b1 - b0) + abs(c1 - c0) == 1 p([m(i) zero]) = p([zero m(i)]); end end end assert(isequal(p,[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;0]))

ans = 19 20 21 22 26 23 25 24 0 ans = 19 20 21 22 23 24 25 26 0 ans = 26 m = Columns 1 through 29 9 18 27 18 9 8 5 2 11 12 15 18 27 26 17 16 25 26 27 26 25 22 13 16 25 26 27 18 9 Columns 30 through 58 8 7 4 13 14 15 18 27 24 15 14 23 24 27 18 9 6 15 18 27 26 25 16 17 18 15 24 27 26 Columns 59 through 87 25 16 17 18 15 24 27 26 25 16 17 18 15 24 27 18 9 8 7 16 17 18 27 26 17 18 15 24 27 Columns 88 through 116 26 17 18 15 24 27 18 9 8 17 18 27 26 23 20 21 24 27 18 17 26 27 24 23 22 25 16 7 8 Columns 117 through 145 17 18 9 8 7 16 25 22 23 24 27 18 17 26 27 26 25 22 19 20 21 24 27 26 25 22 19 20 21 Columns 146 through 174 24 27 26 25 22 19 20 21 24 27 26 25 22 19 20 21 24 27 18 17 16 13 10 19 20 21 24 27 26 Columns 175 through 203 25 16 17 26 27 26 25 22 19 20 21 24 27 26 25 22 19 20 21 24 27 26 25 22 19 20 21 24 27 Columns 204 through 232 26 25 22 19 20 21 24 27 18 17 14 11 20 21 24 27 24 15 18 27 26 25 22 19 20 21 24 27 18 Columns 233 through 261 15 12 21 24 27 26 25 22 19 20 21 24 27 26 25 22 19 20 21 24 27 26 25 22 19 20 21 24 27 Columns 262 through 290 26 25 22 19 20 21 24 27 26 25 22 19 20 21 24 27 26 25 22 19 20 21 24 27 18 17 16 13 22 Columns 291 through 319 23 24 27 18 17 26 27 26 23 20 21 24 27 26 23 20 21 24 27 26 23 20 21 24 27 26 23 20 21 Columns 320 through 348 24 27 18 17 14 23 24 27 26 17 18 27 26 25 22 19 20 21 24 27 18 15 24 27 26 25 22 19 20 Columns 349 through 377 21 24 27 26 25 22 19 20 21 24 27 18 17 16 25 26 27 18 17 26 27 26 23 24 23 24 27 24 23 Columns 378 through 406 22 25 16 17 26 27 18 17 16 25 22 23 24 27 26 25 22 19 20 21 20 19 20 21 24 27 26 25 22 Columns 407 through 435 19 20 21 20 19 20 21 24 27 26 23 24 23 24 27 26 23 20 21 20 21 24 27 26 23 20 21 20 21 Columns 436 through 464 24 27 26 23 20 21 20 21 24 27 26 25 22 23 24 23 22 23 24 27 26 25 22 23 24 23 22 23 24 Columns 465 through 471 27 26 23 24 23 24 27