7
\$\begingroup\$

Your challenge is to output the number of twisted corners given a 2x2 Rubik's Cube scramble.

For the purpose of this challenge, twisted corners are defined as corners where the colour of the top/bottom face before (usually white or yellow) is not facing the top/bottom after the scramble.
Eg. if the cube were to be oriented yellow top/white bottom, then the number of corners where yellow/white isn’t on the top/bottom after the scramble should be returned.

A scrambled cube is represented as a list of moves, where each move is denoted by the letters UDRLFB corresponding to clockwise turns of the faces: Up, Down, Right, Left, Front, and Back.
You have the option to replace the moves UDRLFB with a predefined set of constants. You may assume the scramble list is non-empty.

This is , so the shortest answer wins!
Standard loopholes apply, this loophole in particular is forbidden too.

Test cases

Example gif with scramble "L U U U F" | Top and bottom face after scramble

[In]: R
[Out]: 4
[In]: L U U U F
[Out]: 3
[In]: R R D D D B B U L F
[Out]: 6
[In]: B U U D D F
[Out]: 0
\$\endgroup\$
9
  • \$\begingroup\$ ...not given it a tonne of thought but I think it's the same as for the 2x2 case too, right? \$\endgroup\$ Nov 14, 2023 at 14:01
  • \$\begingroup\$ @JonathanAllan riight, I'll update the question \$\endgroup\$
    – math scat
    Nov 14, 2023 at 14:02
  • 6
    \$\begingroup\$ @close-voters What exactly isn't clear/needs more detail? Please leave a comment rather than just close voting so that the challenge can be improved. \$\endgroup\$
    – math scat
    Nov 14, 2023 at 15:27
  • 1
    \$\begingroup\$ @Fmbalbuena R makes all the corners on the right side twisted and LLL does the same to the left side (as it is just an anticlockwise quarter turn of the left face), so all eight corners end up twisted. \$\endgroup\$ Nov 15, 2023 at 23:41
  • 2
    \$\begingroup\$ @JonathanAllan oh, missed the comment, yes, it’s totally fine! Nice answer btw \$\endgroup\$
    – math scat
    Nov 17, 2023 at 22:00

2 Answers 2

3
\$\begingroup\$

Charcoal, 67 bytes

≔Eχ⁰θFES⌕DBRUFLι«≔I⪪§⪪”)⧴∧\ⅈNυSB⊞ae”⁴ι¹ηUMθ⎇№ηλ﹪⁻ι§θ§η⊖⌕ηλ³κ»I№X⁰θ⁰

Try it online! Link is to verbose version of code. Takes a string of DBRUFL as input. Explanation:

≔Eχ⁰θ

Start with an array of zeros representing the untwisted corners. (There are ten rather than eight to save a byte.)

FES⌕DBRUFLι«

Map the input string into the integers 0-5 and loop over them. (The mapping is contrived to make the calculation below shorter, which is why I didn't require that as the input format.)

≔I⪪§⪪”)⧴∧\ⅈNυSB⊞ae”⁴ι¹η

Look up the corner permutation for this move, which is a list of indices of the corners that move in clockwise order.

UMθ⎇№ηλ﹪⁻ι§θ§η⊖⌕ηλ³κ

For those corners that move, find the corner that moves to this corner, and subtract its value from the move index, modulo 3.

»I№X⁰θ⁰

Output the final number of twisted corners.

22 bytes if I abuse the input format:

≔E⁸ιθFAUMθ§⮌↨ιφκI№÷θχ⁰

Takes input as an array of integers which represent a mapping to apply to the positions of the eight white and yellow stickers. No TIO link because I haven't actually bothered to compute suitable integers.

\$\endgroup\$
2
  • \$\begingroup\$ ooh nice approach! \$\endgroup\$
    – math scat
    Nov 15, 2023 at 12:29
  • \$\begingroup\$ @mathscat I only wish I could have come up with a formula for which corners are cycled for a given move. \$\endgroup\$
    – Neil
    Nov 15, 2023 at 14:05
2
\$\begingroup\$

Jelly,  13  12 bytes

Pushes the I/O rules to the edge (confirmed that it is acceptable)

œ?@ƒ24s3I§ḂS

A monadic Link that accepts a list of permutation indices of the 24 showing facelets* and yields the number of twisted corners.

* i.e. the stickers on older cubes.

Try it online! (The footer converts the usual characters to the defined constants.)

How?

We label the eight corners using the three faces to which they belong in some arbitrary order, starting each with their U/D face and reading anticlockwise (again an arbitrary choice):

    UFR  URB  UBL  ULF  DRF  DBR  DLB  DBR

Now we label these stickers when in a solved state:

    ABC  DEF  GHI  JKL  MNO  PQR  STU  VWX

Now we record the six face-turn permutations:

D:  ABC  DEF  GHI  JKL  VWX  MNO  PQR  STU
L:  ABC  DEF  UST  HIG  MNO  PQR  WXV  LJK
B:  ABC  RPQ  EFD  JKL  MNO  TUS  IGH  VWX
U:  DEF  GHI  JKL  ABC  MNO  PQR  STU  VWX
F:  KLJ  DEF  GHI  XVW  CAB  PQR  STU  NOM
R:  OMN  BCA  GHI  JKL  QRP  FDE  STU  VWX

Now we find the index of each of these in all permutations of the solved state labelling by using code that I wrote that was added to Jelly many moons ago (along with its family including œ?, see below) - TIO:

D:                395176321
U:         5246483101592279
B:     35597574695850514081
F:  81089009208703431936001
U: 270227677129672827133894
R: 376031890064925569220481

Now with a list of these constants as our input, we can perform the permutations in turn and find the number of twisted corners:

œ?@ƒ24s3I§ḂS - Link: list of the defined constants, Scramble
    24       - twenty four
   ƒ         - start with State=24 and reduce using Scramble's Constants:
  @          -   with swapped arguments:
œ?           -     permutation at index {Constant} of the items in {State}
                     (when State=24 it's implicitly cast to [1..24])
      s3     - slice into threes (first of every triple is the U/D facelet)
                 (when Scramble="" the 24 we have is cast to [1..24] giving
                  us [[1,2,3],...,[22,23,34]])
        I    - forward differences of each
         §   - sums
          Ḃ  - modulo two
           S - sum
\$\endgroup\$
1
  • 2
    \$\begingroup\$ @downvoter - I assume this downvote came because of the choice of constants. Please note that I have asked OP about this in a comment. \$\endgroup\$ Nov 16, 2023 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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