# How many twisted corners?

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

[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

• ...not given it a tonne of thought but I think it's the same as for the 2x2 case too, right? Nov 14 at 14:01
• @JonathanAllan riight, I'll update the question Nov 14 at 14:02
• @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. Nov 14 at 15:27
• @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. Nov 15 at 23:41
• @JonathanAllan oh, missed the comment, yes, it’s totally fine! Nice answer btw Nov 17 at 22:00

# Charcoal, 67 bytes

≔Ｅχ⁰θＦＥＳ⌕DBRUFLι«≔Ｉ⪪§⪪”)⧴∧\ⅈNυSＢ⊞ae”⁴ι¹ηＵＭθ⎇№ηλ﹪⁻ι§θ§η⊖⌕ηλ³κ»Ｉ№Ｘ⁰θ⁰


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

≔Ｅχ⁰θ


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

ＦＥＳ⌕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.)

≔Ｉ⪪§⪪”)⧴∧\ⅈNυSＢ⊞ae”⁴ι¹η


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

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


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

»Ｉ№Ｘ⁰θ⁰


Output the final number of twisted corners.

22 bytes if I abuse the input format:

≔Ｅ⁸ιθＦＡＵＭθ§⮌↨ιφκＩ№÷θχ⁰


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.

• ooh nice approach! Nov 15 at 12:29
• @mathscat I only wish I could have come up with a formula for which corners are cycled for a given move.
– Neil
Nov 15 at 14:05

# 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