Кубик Рубика имеет 6 цветов: красный, оранжевый, желтый, белый, синий и зеленый. Красные и оранжевые, желтые и белые, а синие и зеленые лица находятся на противоположных сторонах.
Сеть решенного кубика Рубика выглядит так:
Y
BRGO
W
И плитки выглядят так:
Y Y Y
Y Y Y
Y Y Y
B B B R R R G G G O O O
B B B R R R G G G O O O
B B B R R R G G G O O O
W W W
W W W
W W W
Вызов
При заданных поворотах, обращенных поворотах или двойных поворотах выводится то, во что будет преобразован решенный куб, как искусство ASCII или как изображение (пробелы не нужны, могут существовать или не существовать, конечные пробелы разрешены.).
Ввод будет вращение (и необязательный модификатор). Вращение обозначения идут как: U
(р), L
(EFT), F
(Ront), R
(РАВО), B
(ACK), D
(собственный); 2
(двойной) '
или i
(обратный).
Все нормальные повороты будут на 90 ° по часовой стрелке, обратные будут против часовой стрелки.
Пояснение по часовой стрелке : представьте, что куб смотрит на красное лицо, а желтое лицо вверху. Затем поверните куб так, чтобы лицо, которое будет вращать программа, было обращено к вам. Так будет работать часовой механизм. (За исключением задней стороны, в этом случае вы будете вращать куб горизонтально.)
вход
На входе будет список ходов.
Выход
Искусство ASCII, представляющее куб или изображение сети куба.
Примеры
Ввод : (пусто)
Выход :
Y Y Y
Y Y Y
Y Y Y
B B B R R R G G G O O O
B B B R R R G G G O O O
B B B R R R G G G O O O
W W W
W W W
W W W
Вход : U
(вверх)
Выход :
Y Y Y
Y Y Y
Y Y Y
R R R G G G O O O B B B
B B B R R R G G G O O O
B B B R R R G G G O O O
W W W
W W W
W W W
Ввод : U'
или Ui
(обратный вверх)
Выход :
Y Y Y
Y Y Y
Y Y Y
O O O B B B R R R G G G
B B B R R R G G G O O O
B B B R R R G G G O O O
W W W
W W W
W W W
Вход : U2
(удвоить)
Выход :
Y Y Y
Y Y Y
Y Y Y
G G G O O O B B B R R R
B B B R R R G G G O O O
B B B R R R G G G O O O
W W W
W W W
W W W
Вход : L'
(инвертировано слева)
Выход :
R Y Y
R Y Y
R Y Y
B B B W R R G G G O O Y
B B B W R R G G G O O Y
B B B W R R G G G O O Y
O W W
O W W
O W W
Вход : R
(справа)
Выход :
Y Y R
Y Y R
Y Y R
B B B R R W G G G Y O O
B B B R R W G G G Y O O
B B B R R W G G G Y O O
W W O
W W O
W W O
Вход : U2 L' D
(двойное увеличение, обратное влево, вниз)
Выход :
O Y Y
R Y Y
R Y Y
G B B W O O B B B R R Y
G B B W R R G G G O O Y
O O Y G B B W R R G G G
R O O
W W W
W W W
правила
- Не допускаются лазейки.
- Это код-гольф , поэтому выигрывает самый короткий код в байтах, решающий проблему.
S
,E
,M
,x
,y
,z
,u
/Uw
,d
/Dw
,r
/Rw
,l
/Lw
,f
/Fw
,b
/Bw
moves as well? Or only the default:U
,D
,R
,L
,F
,B
and their counterclockwise variant with apostrophe ('
)? Off-topic: I always wonder with Rubik's Cube related questions, are you also a Twisty Puzzles collector?Ответы:
Ruby,
370 339305 bytesLatest edit: a few bytes saved by rearrangement of the plotting formulas and removal of unnecessary brackets. A massive saving by rewriting the generation of the cube - I never knew Ruby had a builtin for Cartesian Products!
Anonymous function.
Accepts an array of strings, each representing one face turn (a single string with spaces between each face turn is an additional 6 bytes.)
Returns a 9x12 rectangular string.
Brief explanation
This is closely based on a concept from my answer to this question, which in turn was based on a similar concept by Jan Dvorak.
The first line generates a 27 element array, representing the 27 cubies. Each cubie is represented by a 3 dimensional vector in which the sign represents its current position and the magnitude of each coordinate represents the ascii code for the colour of the sticker.
Example move: for R, for each cubie check if the x coordinate is >0 and if so rotate 90 degrees by swapping the y and z coordinates and swapping the sign of one of them.
Take a 9x12 array of spaces and plot the cube into it. For each cubie and axis we check if the sticker exists (coordinate in that axis nonzero), and work out where it is to go. Then we take the coordinate and perform
.abs.chr
to change the number into the required character and plot it.Ungolfed in test program (per 339 byte edit)
Output
источник
Javascript (ES5), 1615 bytes
Ungolfed:
This was a very difficult challenge.
Explanation
Take the example call
s("R U' F")
.The program can only execute x, y and R moves.
U'
is equal toU U U
, so replace that.F
is equal toy y y R y
, so replace that.R U' F'
is therefore equal toR U U U y y y R y
, which the program can run.cstate is defined with a solved cube. A cube is represented by an array containing 6 arrays containing the 9 stickers. The first array is for R, second for U, third for F, D, B, last array is for L. When an y needs to be executed, the program swaps the four arrays of the front, left, back and right face. For an x, it swaps front, down, back and top. Every rotation also rotates the other faces, that were not swapped. A R move rotates the right face and swaps the right part of the front, up, back, down face.
This could be modified to be able to solve problems with all types of moves, by defining them with x, y and R.
источник
R U' F2
gets converted toR U' F F
first, thenR U U U F F
, thenR x y R y y y x x x x y R y y y x x x x y R y y y x x x y y y R y y y y R y
which it then executes? Weird.. but very original. +1 :) How did you came up with this idea?U'
intoU U U
too, butx
es andy
s are really good. I want to steal this too :p'
and2
, and then replaces all moves.C,
17151709168613361328 bytes25 bytes saved thanks to @KevinCruijssen!
No answers so far, so I decided to put my own solution.
Try it online!
Ungolfed old version:
источник
for(b=1;b<8;b++)for(a=1;a<11;a++)r[b][a]=c[b];
tofor(b=1;b<8;)for(a=1;a<11;)r[b][a++]=c[b++];
andfor(i=1;i<=9;i++)
tofor(i=0;++i<=9;)
, and some of the other for-loops as well. Also, theelse if
can be changed to justif
when you checkif(d==66)B(r);if(d==76)L(r);...
And, unlessi
can be negative, you can changeif(i%3==0)
toif(i%3<1)
two times. And I'm sure it can be golfed some more. Nice to see an answer on your own challenge, though. :)if
s andi
though, I'll edited them once I go home. Thanks!else
s bork the program, golfing loops bork the program, the thing withi
works. Thanks anyway.else if
changing toif
fail? :S In each of theelse if
s you compared==##
, so I'm confused why it doesn't. Again, I don't program C, so maybe I'm missing something obvious that C can't do, but still.. Ah well, glad I could help with the modulo<1
instead of==0
Python 3,
610 563 533526 bytes-7 bytes thanks to my colleague rhsmits (really nice
d,*c
form & removal of redundant parentheses)This is a full program.
Superflip and all tests are available at ideone or Try it online!
The program:
YBRGOW
of stickers.r
, which only rotates the stickers on a face, clockwise a quarter-turnU
, which rotates theU
face clockwise a quarter-turn by applyingr
and rotating the stickers on the top-band ofLFRB
clockwise a quarter-turny
, which performsr
on theU
andD
faces and slicesLFRB
- this performs a rotation of the whole cube in the
y
axis (which runs throughU
andD
)- the rotation is clockwise if looking from above
z
, which performs a rotation of the whole cube in thez
axis clockwise a quarter turn looking atR
(the axis runs throughR
andL
) - this time because the way the faces are oriented in our net (same as given in the OP) we have to flip theB
andU
faces over (they switch from the horizontal to vertical parts of the net and vice-versa)input()
, (the moves to be performed) matching a character fromBUDLRF
(actuallyB-U
) possibly followed by a character'i2
'i2
to the number of clockwise turns (the ordinals of these mod6
does the job, with a dummya
to yield1
when none is present)y
andz
, a quarter-turn ofU
(which will now be the face instructed), and then the calls to inverse the sequence of the setup performed. Taking the ordinal of the face character modulo by11
then by7
mapsB:0 U:1 D:2 L:3 F:4 R:5
, allowing a simple indexing into a string of the function name sequences split by spaces.d
to shorten the printingU
andD
Here is the superflip:
источник
Python
760750649 Bytesshamelessly stole the idea of using only 3 rotations from @Paul Schmitz :D
new version:
I mostly just did lots of numpy list slicing and used its built in rotate and roll functions. Input is handled by calling the functions directly with
eval()
ungolfed..
test input:
Comments or suggestions are greatly appreciated :)
источник
of using only 3 rotations
wrong. The program converts any sequence into a sequence with more moves. If you inputF F F F
, it uses more than 3 rotations, as it converts toy y y R y y y y R y y y y R y y y y R y
. It uses three types of rotations.C, 839 bytes
As this is not a full program (function with input from a string argument and output to the console), you need to call it like this:
Use only one call at a time as the function uses and modifies global variables.
Ungolfed:
As you can see, the main idea is to use a fully data-driven approach: The different rotations are expressed as lists of indices that have to be permuted. The permutation code can thus be very short and generic.
источник
f(h,g)int*g;{
-2,l(g,m){
-8,n(char*o){
-5,printf("%s",a)
toputs(a)
-7, also you can replace char constants with their ASCII decimal representation -1 eachCubically, 2 bytes
Try it online!
Explanation:
If extraneous output is allowed, this is an alternative. 1 byte:
Try it online!
Cubically automatically dumps its memory cube to
STDERR
when the program ends. However, it also prints the notepad beforehand.источник
+1
JavaScript (ES6), 820
A porting of the buggy answer by @Paul Schmitz. It's still not completely golfed, but it has the added value that it works.
The main problem in the original answer is that a single function Q is not enough for all the movements involved in a rotation. I had to add 2 other functions O and N. All of them are just called in the right rotation function R.
More readable maybe
источник
update()
is not defined in my console while running the snippet