From 797f92d1174317e2609d2f2ec38b1e2796aabbfd Mon Sep 17 00:00:00 2001 From: koronya Date: Sat, 3 Jan 2026 04:27:14 +0900 Subject: [PATCH] [JS][7kyu] Rubik's Cube Commutators --- .../7kyu/rubik-s-cube-commutators/koronya.js | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 codewars/7kyu/rubik-s-cube-commutators/koronya.js diff --git a/codewars/7kyu/rubik-s-cube-commutators/koronya.js b/codewars/7kyu/rubik-s-cube-commutators/koronya.js new file mode 100644 index 000000000..151b6d2c0 --- /dev/null +++ b/codewars/7kyu/rubik-s-cube-commutators/koronya.js @@ -0,0 +1,21 @@ +// [JS][7kyu] Rubik's Cube Commutators +// rubik-s-cube-commutators +// https://www.codewars.com/kata/6939b1596fb89b7ec3d128ba/train/javascript + +const invertItem = (move) => { + if (move.endsWith('2')) { + return move + } + return move.endsWith("'") ? move.slice(0, -1) : move + "'" +} + +const createCommutator = (a, b) => { + const arrA = a.split(' ') + const arrB = b.split(' ') + + return [...arrA, ...arrB, ...arrA.reverse().map(invertItem), ...arrB.reverse().map(invertItem)].join(' ') +} + +createCommutator('R F', "D2 L'") === "R F D2 L' F' R' L D2" +createCommutator('R', 'U') === "R U R' U'" +createCommutator("F' R U' D", "R U2 F B'") === "F' R U' D R U2 F B' D' U R' F B F' U2 R'"