19
\$\begingroup\$

Scramble a Megaminx!

A megaminx is a dodecahedral version of the familiar Rubik's cube. Since 2008, the World Cube Association uses Stefan Pochmann's megaminx scrambler. The scrambles look like this:

R-- D++ R++ D-- R++ D-- R++ D++ R-- D-- U'
R++ D-- R++ D-- R-- D++ R-- D-- R-- D-- U'
R++ D++ R-- D++ R++ D++ R++ D++ R-- D++ U 
R-- D-- R++ D++ R++ D++ R-- D++ R++ D++ U 
R-- D++ R++ D-- R++ D-- R++ D++ R++ D++ U 
R++ D++ R++ D++ R-- D-- R-- D-- R-- D-- U'
R-- D++ R-- D++ R++ D++ R++ D++ R++ D++ U 

A scramble has seven lines. Each line has 10 R and D moves, starting with R and alternating R and D moves. Following each letter is randomly chosen direction ++ or -- with equal probability. At the end of the line is randomly chosen with equal probability U (trailing space form U allowed) or U'. A trailing newline at the end of the output is permitted.

Your code must take no input and must produce a random scramble.

\$\endgroup\$
1
  • \$\begingroup\$ Woops.. I accidently deleted my comment. So I'll just ask it again.. Are we allowed to output in lowercase (rdu) instead of uppercase? \$\endgroup\$ Dec 14 '20 at 15:35

21 Answers 21

4
\$\begingroup\$

Ruby, 73 71 bytes

Saved two bytes thanks to @vrintle (one directly and another with further golfing)

77.times{|i|$><<(~i%11>0?:RD[i%2]+'+-'[rand 2]*2+' ':?U+?'*rand(2)+$/)}

Try it online!

$/ is the input record separator, which is a newline by default.


In Ruby 2.7 (newer than the version on TIO), we get 70 bytes using numbered block parameters:

77.times{$><<(~_1%11>0?:RD[_1%2]+'+-'[rand 2]*2+' ':?U+?'*rand(2)+$/)}
\$\endgroup\$
4
  • 1
    \$\begingroup\$ 72 bytes or 72 bytes, whichever you can golf further ;-) \$\endgroup\$
    – vrintle
    Dec 13 '20 at 3:51
  • 1
    \$\begingroup\$ @vrintle Very nice. Saved another byte with a predefined variable :) \$\endgroup\$
    – Dingus
    Dec 13 '20 at 3:59
  • \$\begingroup\$ btw, why are you preventing Ruby 2.7? \$\endgroup\$
    – vrintle
    Dec 13 '20 at 7:15
  • 1
    \$\begingroup\$ @vrintle Force of habit mainly. I do like numbered parameters but it's mildly annoying having to modify TIO's copy-paste template. \$\endgroup\$
    – Dingus
    Dec 13 '20 at 8:27
4
\$\begingroup\$

Python 2, 91 bytes

from random import*
for C in[choice]*7:print' '.join(c+C('+-')*2for c in'RD'*5),'U'+C("' ")

Try it online!

Alternatively:

from random import*
C=choice
exec'print" ".join(c+C("+-")*2for c in"RD"*5),"U"+C("\' ");'*7

Try it online!

Python 2, 92 bytes

from random import*
C=choice
for c in('RD'*5+'U')*7:print c+[C("+-")*2,C("' ")+"\n"][c>"T"],

Try it online!

Python 2, 92 bytes

from random import*
for C in[choice]*7:
 for c in'RD'*5:print c+C('+-')*2,
 print'U'+C("' ")

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ 86 bytes with Python 3.8 \$\endgroup\$
    – vrintle
    Dec 15 '20 at 3:24
  • \$\begingroup\$ The two for loops answer is surprisingly readable for being golved. \$\endgroup\$
    – qwr
    Dec 16 '20 at 7:23
3
\$\begingroup\$

Perl 5, 68 bytes

say+(map{map$_.("++ ","-- ")[rand 2],R,D}0..4),U,"'"x rand 2for 0..6

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ Nice one! With a couple of minor tweaks you can save a few bytes too: Try it online! \$\endgroup\$ Dec 23 '20 at 21:31
3
\$\begingroup\$

C (gcc), 135 132 124 123 119 bytes

  • -12 thanks to ceilingcat
  • -1 by rearranging the inner for terms

For each line, I start with R++ and then flip R=>D and ++=>-- (half the time). This continues ten times and concludes with either U or U'. Instead of doing string operations, I use XOR for all the manipulations and print the integer as a reinterpreted string.

f(j){long i,k=7;for(srand(time(0));k--;puts(rand()%2?"U'":"U"))for(j=10,i=' ++R';j--;printf(&i))i^=22+rand()%2*394752;}

Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ wdym randomly flipping between ++ and --? Do you mean there is a 50% chance that the sign will change? Then that should be valid. \$\endgroup\$
    – qwr
    Dec 13 '20 at 6:53
2
\$\begingroup\$

[Python 3.6], 144 121 120 111 bytes

Huge reduction thanks to Sisyphus, and qwr

from random import*
d=choice
for r in range(35):print(end=f"R{d('-+')*2} D{d('-+')*2} "+r%5//4*f"U{d(' !')}\n")

Try it online

Each time through the loop prints one pair of entries, one R and one D. The r%5//4*f* control takes care of conditionally printing the U entry and the end of line.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ 114 bytes using random.choice over string seq. Could be lower if I weren't too lazy to figure out how to encode the ' in the f-string. \$\endgroup\$
    – qwr
    Dec 13 '20 at 6:51
  • 1
    \$\begingroup\$ 111 bytes if we cheat and use a symbol like i instead of '. The i notation or using the "prime symbol" is valid in terms of scrambles but no one does it. \$\endgroup\$
    – qwr
    Dec 13 '20 at 7:06
  • \$\begingroup\$ The 111 version works w/ the ! symbol so I left that, for consistency with the problem description. \$\endgroup\$
    – cnamejj
    Dec 13 '20 at 9:08
  • 1
    \$\begingroup\$ I use i because it was formerly common move notation and ! isn't. But the difference isn't big. \$\endgroup\$
    – qwr
    Dec 13 '20 at 9:19
2
\$\begingroup\$

R, 87 bytes

s=sample;for(i in 1:7)cat(paste0(c("R","D"),s(c("++","--"),10,T)),s(c("U","U'"),1),"
")

Try it online!

R's paste0 function (to paste text strings together without separator) is conveniently vectorized, so we can exploit R's element recycling when the two arguments are different lengths: in this case, the 2-element vector R,D is automatically recycled 5 times when paste0-ed with the 10-element vector created by sample-ing ++,-- ten times. The T argument to sample indicates that sampling with replacement is True (needed to take 10 samples from 2-elements).

\$\endgroup\$
1
  • \$\begingroup\$ nice use of recycling. \$\endgroup\$
    – qwr
    Dec 14 '20 at 0:12
2
\$\begingroup\$

Retina, 40 bytes


7*$(5*$(R-- D-- )U'¶
/--/_?&K`++
%?&`'

Try it online! Explanation:


7*$(5*$(R-- D-- )U'¶

Insert 7 lines consisting of R-- D-- repeated 5 times followed by U'.

/--/_?&K`++

Match each -- and then randomly replace each one with ++.

%?&`'

On each line, randomly delete '.

\$\endgroup\$
2
\$\begingroup\$

APL (Dyalog Unicode), 56 bytes

''' '[?7/2],⍨'U',⍨7 10⍴,/⍉2 70⍴(2/¨'+-')[?70/2],⍨70⍴'RD'

Try it online!

\$\endgroup\$
1
\$\begingroup\$

JavaScript (ES6), 92 bytes

f=k=>k>69?'':'RD'[k&1]+((r=Math.random()*4)&1?'++ ':'-- ')+(k%10>8?r&2?`U'
`:`U
`:'')+f(-~k)

Try it online!


JavaScript (ES6), 93 bytes

A non-recursive approach.

_=>`RDRDRDRDRDU
`.repeat(7).replace(/./g,c=>c+[['++ ','-- ',"'"][Math.random()*2|2*(c>'R')]])

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ Interesting - how does the k variable work? \$\endgroup\$
    – qwr
    Dec 13 '20 at 0:37
  • \$\begingroup\$ @qwr It's undefined during the first iteration. So, k&1 is 0 and k%10 is NaN (falsy). Then -~k sets it to 1 and the weirdness ends here. \$\endgroup\$
    – Arnauld
    Dec 13 '20 at 0:47
  • \$\begingroup\$ I love and hate the idea of having undefined variables used \$\endgroup\$
    – qwr
    Dec 13 '20 at 1:12
1
\$\begingroup\$

Charcoal, 26 bytes

E⁷⪫⮌E¹¹⎇λ⁺§RDλײ‽+-…U'⊕‽² 

Try it online! Link is to verbose version of code. Explanation:

 ⁷                          Literal `7`
E                           Map over implicit range
     ¹¹                     Literal `11`
    E                       Map over implicit range
           RD               Literal string `RD`
          §  λ              Indexed by inner index
         ⁺                  Concatenated with
                 +-         Literal string `+-`
                ‽           Random character
              ײ            Duplicated
       ⎇λ                   Except for the first entry use
                    U'      Literal string `U'`
                   …        Truncated to length
                       ‽²   Random integer less than 2
                      ⊕     Incremented
   ⮌                        Reversed (so `U` is now last)
  ⪫                         Joined with spaces
                            Implicitly print on separate lines
\$\endgroup\$
1
\$\begingroup\$

Python 3, 113 97 126 bytes

Saved 6 bytes thanks to qwr!!!
Added 29 bytes to fix an error kindly pointed out by qwr.

from random import*
f=lambda n=5,r=choice:n and f"R{r('-+')*2} D{r('-+')*2} "+f(n-1)or'U'+r(" '")
for i in range(7):print(f())

Try it online!

\$\endgroup\$
7
  • \$\begingroup\$ 112 bytes using random.random and r()>.5 \$\endgroup\$
    – qwr
    Dec 13 '20 at 6:39
  • \$\begingroup\$ Are you sure your TIO is correct? Don't you need to include the footer as bytes? \$\endgroup\$
    – qwr
    Dec 13 '20 at 6:42
  • \$\begingroup\$ anyway, 97 bytes using random.choice \$\endgroup\$
    – qwr
    Dec 13 '20 at 6:44
  • \$\begingroup\$ @qwr Nice one - thanks! No, you don't include the header or footer. Just the solution to the challenge. \$\endgroup\$
    – Noodle9
    Dec 13 '20 at 10:18
  • 1
    \$\begingroup\$ but your function needs to produce 7 lines of output \$\endgroup\$
    – qwr
    Dec 13 '20 at 10:22
1
\$\begingroup\$

Jelly, 34 bytes

7µ5,6ṁ11ż2Xx¤€$+0¦2ị“+-' RDU”KFṖ)Y

Try it online!

How?

7µ5,6ṁ11ż2Xx¤€$+0¦2ị“+-' RDU”KFṖ)Y - Link: no argments
7                                  - seven
 µ                              )  - for each (v in [1..7]):
  5,6                              -   [5,6]
     ṁ11                           -   mould like 11 -> [5,6,5,6,5,6,5,6,5,6,5]
              $                    -   last two links as a monad:
             €                     -     for each:
            ¤                      -       nilad followed by link(s) as a nilad:
         2                         -         two
          X                        -         random integer (from [1,2])
           x                       -         repeat (two) times -> [1,1] or [2,2]
        ż                          -     zip together -> [[5,[2,2]],[6,[1,1],...]
                0¦                 -   apply to index 0 (rightmost):
               +  2                -     add two (e.g. [5,[1,1]] -> [7,[3,3]])
                    “+-' RDU”      -   "+-' RDU"
                   ị               -   index into (i.e. 1 -> '+' ... 7 -> 'U')
                             K     -   join with space characters
                              F    -   flatten
                               Ṗ   -   pop (remove the repeated space or quote from the right)
                                 Y - join with newline characters
\$\endgroup\$
1
\$\begingroup\$

Pyth, 28 bytes

V7jd+m+@"RD"d*2O"+-"T+\UO" '

Try it online!

V7jd+m+@"RD"d*2O"+-"T+\UO" '   
V7                             Perform the following 7 times:
     m              T          Map d over 0-9:
               O"+-"             Choose + or - at random
             *2                  Double it
       @"RD"d                    Choose R for even d, D for odd
      +                          Concatenate the previous two results
                        O" '   Choose space or ' at random
                     +\U       Append to "U"
    +                          Append the previous result to the list of moves
  jd                           Join the list on spaces, implicit print with newline
\$\endgroup\$
1
\$\begingroup\$

MathGolf, 24 23 bytes

7{ûRD5*môû+-w∞ ûU'1w╛╡n

Try it online.

Could be 22 bytes if we're allowed to output in lowercase instead of uppercase, by changing the ûRD to ╢x (and U' to u'): try it online.

Explanation:

7{                       # Loop 7 times:
  ûRD                    #  Push string "RD"
     5*                  #  Repeat it 5 times: "RDRDRDRDRD"
       m                 #  Map over each character,
        ô                #  using the following six character as inner code-block:
         û+-             #   Push string "+-"
            w            #   Pop and push a random character from this string
             ∞           #   Double it
                         #   Push a space character " "
               ûU'      '#  Push string "U'"
                  1w     #  Push a random integer in the range [0,1]
                    ╛    #  If this integer is truthy (thus 1):
                     ╡   #   Discard the right character of the string
                      n  #  Push a newline character "\n"
                         # (after the loop, the entire stack joined together is output
                         #  implicitly as result)
\$\endgroup\$
1
\$\begingroup\$

05AB1E, 25 bytes

Credit goes to Kevin Cruijssen for coming up with this way shorter program!

„RDÞε„+-Ωº«}Tôε„U'ηΩª}7£»

Try it online!

How this?

„RDÞ                                        # Push RDRDRDRDRDRDRDRDRDRD... as a list
    ε      }                                # For every R or D:
     „+-Ω                                   #   Pick random from + and -
         º                                  #   Double the sign
          «                                 #   And join it to the R or D
            Tô                              # Split the list into pieces of 10 elements
              ε      }                      # For each item in the list:
               „U'ηΩ                        #   Choose a random choice between U' & U
                    ª                       #   Append it to the list
                      7£»                   # Join the first 7 lists with linefeeds
                                            # Print the result at the end (implicitly)!

My old program:

05AB1E, 36 bytes

8G„RD5×S"++ -- "3ôTи2ô€ΩøJ…U'U2ôΩªJ,

Try it online!

How that?

8G                                          # Repeat 7 times the full code
  „RD5×                                     # Push "RDRDRDRDRD"
        "++ -- "3ô                          # Push the list/array ["++ ", "-- "]
                  Tи2ô                      # Repeat the list 10 times
                      €Ω                    # Pick a random item from each list
       S                øJ        J         # Zip the random items with "RDRDRDRDRD"
                                            # e.g => "R++ D-- R++ D-- R++ D-- R++ D-- R++ D-- "
                          …U'U2ôΩ           # Pick random between "U'" and "U"
                                 ª          # Append the result to the last result
                                  ,         # Print it with a linefeed
\$\endgroup\$
1
1
\$\begingroup\$

Actually, 40 bytes

7"RD""35"≈*⌠"+-"Jτ+R⌡M╡⌠2"UU'"╡J@+' j⌡Mi

Try it online!

 "RD"                                    # Push RD,
     "35"≈*                              # and repeat it 35 times.
           ⌠        ⌡M                   # For each turn,
           ⌠"+-"J   ⌡M                   # pick either + or -,
           ⌠     τ  ⌡M                   # double it,
           ⌠      +R⌡M                   # and add it to the end of each turn.
7                     ╡                  # Split the list into 7 sublists (of length 10).
                       ⌠             ⌡M  # For each sublist,
                       ⌠2"UU'"╡J     ⌡M  # pick either U or U',
                       ⌠        @+   ⌡M  # add it to the end of each turn,
                       ⌠          ' j⌡M  # and join with spaces.
                                       i # Push each sublist to the stack.
                                         # Implicitly output the stack joined with newlines.
\$\endgroup\$
1
\$\begingroup\$

Python 3.8 (pre-release), 86 bytes

A golfed version of xnor's answer, which he didn't approved. Perhaps, because his answer is in Python 2.

from random import*
for C in[choice]*7:print(*(c+C('+-')*2for c in'RD'*5),'U'+C("' "))

Try it online!

\$\endgroup\$
1
\$\begingroup\$

><>, 78, 75 bytes

Saved 3 bytes by modifying the jump in the third line and changing how the register was checked and decremented.

7_a*&"DR"d1.  >x"-"v
>x"'"oao&:&?!;^\"+">$:o$:oo$" "od1&1-:&a%?.~~"U"o
.>51

New link: Try it online!

Old link: Try it online!

This code also runs in *><> (Starfish) because *><> is a super set of ><>.

*><>: Try it online!

\$\endgroup\$
1
\$\begingroup\$

Vyxal, 25 bytes

₀(‛RD5*ƛ‛+-℅d+;\U‛U'"℅JṄ,

Try it Online!

Explanation

₀(‛RD5*ƛ‛+-℅d+;\U‛U'"℅JṄ,
 (                         For n
₀                          in range(10):
  ‛RD                        Push "RD"
     5*                      Repeat 5 times
       ƛ      ;              Map:
        ‛+-                    Push "+-"
           ℅                   Choose random
            d                  Double
             +                 Concatenate top two
               \U            Push "U"
                 ‛U'         Push "U'"
                    "        Join top two into a pair
                     ℅       Choose random
                      J      Join top two
                       Ṅ     Join with spaces
                        ,    Print
\$\endgroup\$
0
\$\begingroup\$

Nim, 129 bytes

import random
randomize()
for _ in 0..6:
 var l=["U","U'"].sample
 for i in 0..9:l="RD"[i mod 2]&["++","--"].sample&" "&l
 echo l

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Jelly, 28 27 bytes

⁵µ⁾RDẋ5;⁾+-XḤ¤$€“U“U'”X¤ṭ)G

Try it online!

Explanation

⁵µ⁾RDẋ5;⁾+-XḤ¤$€“U“U'”X¤ṭ)G  Main niladic link
⁵                            10 [implicitly casted to [1..10]]
 µ                       )   Map:
  ⁾RD                          "RD"
     ẋ5                        Repeat 5 times
               €               Map
              $                (
       ;                         Join with
             ¤                   (
        ⁾+-                        "+-"
           X                       Choose random
            Ḥ                      Unhalve (double)
             ¤                   )
              $                )
                        ṭ      Append
                       ¤       (
                “U“U'”           ["U", "U'"]
                      X          Choose random
                       ¤       )
                          G  Format as a grid
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.