23
\$\begingroup\$

Super Mario Galaxy features two rhombicuboctahedron-shaped* planets tiled with platforms that shrink as Mario runs across. Should Mario fall into a triangular hole or a gap left by a tile he previously touched, he will be consumed by the black hole at the core. (Watch: Hurry-Scurry Galaxy, Sea Slide Galaxy)

Image: MarioWiki.com

Image: MarioWiki.com

(You can think of the planet as a 2x2x2 cube whose faces have been detached and connected to each other by 2x3 "bridges".)

Unfortunately, since my controller is very broken, Mario cannot jump and is limited to the four cardinal directions. Additionally, Mario moves very slowly and cannot retrace even one step without first having the platform behind him disappear.

Let's assume the camera is always above Mario's head and he starts at the bottom right of a 2x2 face:

      ■ ■
      ■ ■
      ■ ■
■ ■ ■ ■ ■ ■ ■ ■
■ ■ ■ ■ M ■ ■ ■
      ■ ■
      ■ ■
      ■ ■

Your program will take a list or string of directions, U D L R (up, down, left, right), representing Mario's walk around the planet so far as a series of steps. The program can output one of two distinct outputs: one representing that Mario is still alive and walking, and the other representing that somewhere along his walk, Mario has fallen into the Shrinking Satellite.

RR:   ■ ■                 RRD:  ■ ■                 RRL:  ■ ■      
      ■ ■                       ■ ■                       ■ ■      
      ■ ■                       ■ ■                       ■ ■      
■ ■ ■ ■ ■ ■ ■ ■           ■ ■ ■ ■ ■ ■ ■ ■           ■ ■ ■ ■ ■ ■ ■ ■
■ ■ ■ ■ □ □ M ■           ■ ■ ■ ■ □ □ □ ■           ■ ■ ■ ■ □ M □ ■
      ■ ■    \                  ■ ■   M                   ■ ■  \
      ■ ■     Let's-a go!       ■ ■    \                  ■ ■   W-aaaaaaaaaahh!
      ■ ■                       ■ ■     W-aaaaaaaaaahh!   ■ ■

Of course, unlike the above diagrams, you'll have to take the 3D into account. Here's a diagram that might help you visualize the scenario better:

                Top 2x2 face
   <- clockwise           anticlockwise ->
   -   ■    -    ■    -    ■    -    ■   -
     /   \     /   \     /   \     /   \  
   ■       ■ ■       ■ ■       ■ ■       ■
   ■       ■ ■       ■ ■       ■ ■       ■
   ■       ■ ■       ■ ■       ■ ■       ■
   ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■    Left and right
   ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ M ■ ■ ■ ■ ■ ■ ■ ■ ■    edges wrap around.
   ■       ■ ■       ■ ■       ■ ■       ■
   ■       ■ ■       ■ ■       ■ ■       ■
   ■       ■ ■       ■ ■       ■ ■       ■
     \   /     \   /     \   /     \   /  
   -   ■    -    ■    -    ■    -    ■   -
   <- anticlockwise           clockwise ->
               Bottom 2x2 face

So according to this diagram, UUUUURRRR might look like this:

   -   ■    -    ■    -    □    -    ■   -
     /   \     /   \     /   \     /   \  
   ■       ■ ■       ■ □       □ ■       ■
   ■       ■ ■       ■ □       □ ■       ■
   ■       ■ ■       ■ □       □ ■       ■
   ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ □ ■ ■ ■ M ■ ■ ■ ■ ■
   ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ □ ■ ■ ■ ■ ■ ■ ■ ■ ■
   ■       ■ ■       ■ ■       ■ ■       ■
   ■       ■ ■       ■ ■       ■ ■       ■
   ■       ■ ■       ■ ■       ■ ■       ■
     \   /     \   /     \   /     \   /  
   -   ■    -    ■    -    ■    -    ■   -

And UUUUUUUUULURRRRRR might look like this:

   -   ■    -    ■    -    □    -    □   -
     /   \     /   \     /   \     /   \  
   ■       ■ ■       ■ □       ■ ■       □
   ■       ■ ■       ■ □       ■ ■       □
-> □       ■ ■       ■ □       ■ ■       □ ->
<- □ ■ ■ ■ ■ ■ ■ ■ ■ ■ □ ■ ■ ■ M □ □ □ □ □ <-
   ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ □ ■ ■ ■ ■ ■ ■ ■ ■ ■
   ■       ■ ■       ■ ■       ■ ■       ■
   ■       ■ ■       ■ ■       ■ ■       ■
   ■       ■ ■       ■ ■       ■ ■       ■
     \   /     \   /     \   /     \   /  
   -   ■    -    ■    -    ■    -    ■   -

May the shortest program in bytes w-aaaaaaaaaahh!

Test Cases

Output 1: Still Alive

DDDDDLUUUUU - Mario walks across a bridge and back.

RRRRDDDDLLL - Mario walks in a triangle.

LLLLLLUUUUUURRRRR - Mario walks in a bigger triangle.

ULLDRDDDRU - Mario puts himself in peril.

RURDRURDRDLDRDLDLDLULDLLLLLLLLLLLLLLLLURRRRRRRRRRRRRR - Mario takes an unconventional route... and puts himself in peril.

Mario crosses every tile exactly once. DDDDLUUUULLLLDDDLUUULLLLDDDDLUUUULLLLDDDLUUULLLURRRUUURDDDRRRRUUURDDDRRRRUUURDDDRRRRUUUUURDDDDD DLDRDLDLLLDRRRDDDDLLLLLLLLLDRRRRRRRRRDDDDLLLDRRRDDDRUUURRRRULLLLUUUURRRULLLUUUUURDRURDRUURULURU

Output 2: W-aaaaaaaaaahh!

LLR - Mario attempts to retrace a step and falls off.

UULDR - Mario attempts to cross a tile twice and steps into air.

RRDDDDD - Mario walks off a bridge at the first D (ignore any following steps).

RRRRDDDDLLLL - Mario walks in a triangle and falls through the starting tile.

LLLLLLUUUUUURRRRRR - Mario walks in a bigger triangle and falls through the starting tile.

UUUUUUUUUUUUUUUUUUUU - Mario walks all the way around the planet and falls through the starting tile.

RURDRURDRDLDRDLDLDLULDLLUU - Mario takes an unconventional route and becomes disoriented.

Mario, realizing the peril he is in, is left with no choice.

ULLDRDDDRUUU ULLDRDDDRUUL ULLDRDDDRUUR ULLDRDDDRUUD RURDRURDRDLDRDLDLDLULDLLLLLLLLLLLLLLLLURRRRRRRRRRRRRRR RURDRURDRDLDRDLDLDLULDLLLLLLLLLLLLLLLLURRRRRRRRRRRRRRU RURDRURDRDLDRDLDLDLULDLLLLLLLLLLLLLLLLURRRRRRRRRRRRRRL RURDRURDRDLDRDLDLDLULDLLLLLLLLLLLLLLLLURRRRRRRRRRRRRRD

Finally, copy any test case from "Mario crosses every tile exactly once" and change or add one step at random. Mario should fall. (If you add a step to the end, Mario falls to grab the Power Star!)

*Cantellated cube would be a more correct term as some faces aren't square, but you've got to admit - "rhombicuboctahedron" flows nicer.

\$\endgroup\$
  • 3
    \$\begingroup\$ Bonus points for solving this in Cubix or Cubically \$\endgroup\$ – Stephen Aug 2 '17 at 22:44
  • \$\begingroup\$ This brings back so many memories of playing Mario Galaxy - easily one of my favorite games of all time. \$\endgroup\$ – notjagan Aug 2 '17 at 22:45
  • 7
    \$\begingroup\$ @StepHen Or MarioLANG :P \$\endgroup\$ – ETHproductions Aug 3 '17 at 0:51
  • \$\begingroup\$ @Stephen even though it's an octogon, hexagony sti... Y'know what, never mind, who'd do this in hexAgony. \$\endgroup\$ – Magic Octopus Urn Aug 3 '17 at 19:04
  • \$\begingroup\$ In the 4th from last test case, Mario doesn't die unless you add an extra R. I have worked this out on paper to be sure my code is correct. \$\endgroup\$ – Level River St Aug 7 '17 at 12:15
6
\$\begingroup\$

Ruby, golfed, 244 230 bytes

Seems to work fine, will test a bit more.

->s{a=[8**8/5]*8
v=[-1,x=d=0,1,0]
y=m=4
s.chars{|c|a[y]&=~(1<<x) 
y+=v[e="URDL".index(c)+d&3]
x+=v[e-1]
r= ~0**q=x/4
i=q+x&1
j=q+y&1
y%9>7&&(y=4-i;x+=4-j*11-x%2;d+=r)
x&2>0&&-y/2==-2&&(y=i*7;x+=6-x%2*9+j;d-=r)
m*=1&a[y]>>x%=24}
m}

Ruby, first working version, 260 bytes

Try it online

Lambda function taking a string argument. Returns 4 for alive, 0 for dead.

->s{a=[0x333333]*8
v=[0,-1,0,1]
x=d=0
y=m=4
s.chars{|c|a[y]&=~(1<<x) 
e="URDL".index(c)+d
x+=v[e%4]
y+=v[-~e%4]
p=x&-2
q=x/4%2
y%9>7&&(d-=q*2-1;y,x=4-(q+x)%2,(p+4-(q+y)%2*11)%24)
x&2>0&&-y/2==-2&&(y,x=(q+x)%2*7,(p+6-x%2*8+(q+y)%2)%24;d+=q*2-1)
m*=a[y]>>x&1}
m}

Explanation

The board is unfolded into 6 strips of size 2x8, represented by the /\ and O characters below. These are mapped onto a 24*8 2D map, where x=(strip number)*4+(horizontal position on strip) and y=vertical position on strip.

       Map        4         2         0          Initial state of array a
                 /         /         /   
                / /       / /       / /          1100110011001100110011
               / /       / /       / /           1100110011001100110011 
              O /       O /       O /            1100110011001100110011
             O O       O O       O O             1100110011001100110011
      \     / O \     / O \     / X              110011001100110011001X
     \ \   / / \ \   / / \ \   / /               1100110011001100110011
      \ \ / /   \ \ / /   \ \ / /                1100110011001100110011
       \ O /     \ O /     \ O /                 1100110011001100110011
        O O       O O       O O 
         O \       O \       O \                 X=Mario's start point 
          \ \       \ \       \ \  
           \ \       \ \       \ \    
            \         \         \
             5         3         1

These are stored in an array of 8 binary numbers, so x increases going left and y increases going down.

The array is intialized with 8 copies of the number 0x33333333. This forms the squares Mario is allowed to step on. As Mario moves around the square he is on is set to zero, and the square he is moving to is tested - he lives it contains a 1, and dies if it contains a 0.

If Mario walks off the top or bottom of the strip he is on, he moves to another strip. If he walks off the side of the strip he is on, if he is on a square with y=3 or y=4 he moves to another strip. If y is not 3 or 4, he does not move to another strip and ends up on a square which had 0 in it from the outset of the game, so he dies.

Because the camera is always above Mario's head, whenever Mario changes strip, the reference for the directions must be rotated by 90 degrees.

Ungolfed in test program

f=->s{                             #Move sequence is taken as string argument s.
  a=[0x333333]*8                   #Setup board as an array of 8 copies of 1100110011001100110011.
  v=[0,-1,0,1]                     #Displacements for moving.
  x=d=0                            #Mario starts at 0,4.
  y=m=4                            #d=offset for directions. m=4 when Mario is alive (value chosen for golfing reasons) 0 when dead.

  s.chars{|c|                      #For each character c in s
    a[y]&=~(1<<x)                  #Set the square where Mario is to 0.

    e="URDL".index(c)+d            #Decode the letter and add the offset 
    x+=v[e%4]                      #x movement direction is v[e%4]   
    y+=v[-~e%4]                    #y movement direction is v[(e+1)%4]
    p=x&-2                         #p is a copy of x with the last bit set to zero (righthand edge of strip).
    q=x/4%2                        #q is 0 for an even number strip, 1 for an odd number strip.
    y%9>7&&(                       #If y out of bounds (8 or -1)
      d-=q*2-1;                    #Adjust d so directions will be interpreted correctly on the next move.
      y,x=
        4-(q+x)%2,                 #y becomes 3 or 4 depending on the values of q and x.
        (p+4-(q+y)%2*11)%24        #If q+y is even, move 1 strip left. if even, move 2 strips right. Take x%24.  
    )
    x&2>0&&-y/2==-2&&(             #If x&2>0 Mario has walked sideways off a strip. If y is 3 or 4, move him to a different strip.
      y,x=                       
        (q+x)%2*7,                 #y becomes 0 or 7, depending on the values of q and x.
        (p+6-x%2*8+(q+y)%2)%24;    #If x%2 is even, move 2 strips left. If odd, move 1 strip right*. Pick the left or right column of the strip depending on (q+y)%2. Take x%24 
        d+=q*2-1                   #Adjust d so directions will be interpreted correctly on the next move.
    )

    m*=a[y]>>x&1                   #Multiply m by the value (0 or 1) of the current square. Mario is either alive (4) or dead (0).  
    #puts x,y,m,a.map{|i|"%022b"%i}#Uncomment this line for diagnostics.
  }
m}                                 #Return value of m.  


#Still alive, return value 4
puts f["DDDDDLUUUUU"] # Mario walks across a bridge and back.
puts f["RRRRDDDDLLL"] # Mario walks in a triangle.
puts f["LLLLLLUUUUUURRRRR"] # Mario walks in a bigger triangle.
puts f["ULLDRDDDRU"] # Mario puts himself in peril.
puts f["RURDRURDRDLDRDLDLDLULDLLLLLLLLLLLLLLLLURRRRRRRRRRRRR"] # Mario takes an unconventional route... and puts himself in peril.
puts f["DDDDLUUUULLLLDDDLUUULLLLDDDDLUUUULLLLDDDLUUULLLURRRUUURDDDRRRRUUURDDDRRRRUUURDDDRRRRUUUUURDDDDD"] 
puts f["DLDRDLDLLLDRRRDDDDLLLLLLLLLDRRRRRRRRRDDDDLLLDRRRDDDRUUURRRRULLLLUUUURRRULLLUUUUURDRURDRUURULURU"]

#Dead, return value 0

puts f["LLR"] #  Mario attempts to retrace a step and falls off.
puts f["UULDR"] #  Mario attempts to cross a tile twice and steps into air.
puts f["RRDDDDD"] #  Mario walks off a bridge at the first D (ignore any following steps).
puts f["RRRRDDDDLLLL"] #  Mario walks in a triangle and falls through the starting tile.
puts f["LLLLLLUUUUUURRRRRR"] #  Mario walks in a bigger triangle and falls through the starting tile.
puts f["UUUUUUUUUUUUUUUUUUUU"] #  Mario walks all the way around the planet and falls through the starting tile.
puts f["RURDRURDRDLDRDLDLDLULDLLUU"] # 

puts f["ULLDRDDDRUUU"] 
puts f["ULLDRDDDRUUL"] 
puts f["ULLDRDDDRUUR"] 
puts f["ULLDRDDDRUUD"] 
puts f["RURDRURDRDLDRDLDLDLULDLLLLLLLLLLLLLLLLURRRRRRRRRRRRRRR"] #text case in q is wrong. one more R added to make the kill.
puts f["RURDRURDRDLDRDLDLDLULDLLLLLLLLLLLLLLLLURRRRRRRRRRRRRU"] 
puts f["RURDRURDRDLDRDLDLDLULDLLLLLLLLLLLLLLLLURRRRRRRRRRRRRL"] 
puts f["RURDRURDRDLDRDLDLDLULDLLLLLLLLLLLLLLLLURRRRRRRRRRRRRD"]
\$\endgroup\$
  • \$\begingroup\$ Nicely done! I really like the "strip" mapping and the way you accounted for the camera angle. \$\endgroup\$ – darrylyeo Aug 8 '17 at 14:55

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.