rcuber 0.7.20

crate for rubiks cube and solver (LBL, CFOP, Roux, min2phase)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
use crate::cubie::{Corner, CubieCube};
use crate::facelet::Color;
use crate::moves::Formula;
use crate::moves::Move::{self, *};
use std::collections::HashMap;

/// `CMLL` is the third step of the Roux method, after solving the first two blocks. The goal of this step is to solve the corners of the last layer without considering the M-slice.
/// In contrast to other algorithm sets like COLL, this algorithm set IS allowed to disturb the M-slice. This gives more freedom and allows more efficient algorithms for some cases.
/// # Example
/// ```rust
/// use rcuber::cubie::CubieCube;
/// use rcuber::moves::Formula;
/// use rcuber::solver::roux::SolverBase;
/// use rcuber::solver::roux::fb::FBSolver;
/// use rcuber::solver::roux::sb::SBSolver;
/// use rcuber::solver::roux::cmll::CMLLSolver;
///
/// fn main() {
///     let cc = CubieCube::default();
///     let f = Formula::scramble();
///     let cc = cc.apply_formula(&f);
///     let mut fb = FBSolver::new(cc);
///     let _fb = fb.solve();
///     assert!(fb.is_solved());
///     let mut sb = SBSolver::new(fb.cube);
///     let _sb = sb.solve();
///     assert!(sb.is_solved());
///     let mut cmll = CMLLSolver::new(sb.cube);
///     let _cmll = cmll.solve();
///     assert!(cmll.is_solved());
///     println!("
///         Scramble: {:?}\nFirst Block: {:?}\nSecond Block: {:?}\nCMLL: {:?}",
///         f.moves, _fb, _sb, _cmll
///     );
/// }
/// ```

pub struct CMLLSolver {
    pub cube: CubieCube,
    algos: HashMap<String, Vec<Move>>,
}

impl CMLLSolver {
    /// Construct the CMLLSolver.
    pub fn new(cube: CubieCube) -> Self {
        let mut algos = HashMap::new();
        // solved
        algos.insert("RFFLLBBR".to_string(), vec![]);
        // o_adjacent_swap
        algos.insert(
            "BRFLLBRF".to_string(),
            vec![R, U, R3, F3, R, U, R3, U3, R3, F, R2, U3, R3],
        );
        // o_diagonal_swap
        algos.insert(
            "LBFLRFBR".to_string(),
            vec![F, R, U3, R3, U3, R, U, R3, F3, R, U, R3, U3, R3, F, R, F3],
        );
        // h_columns
        algos.insert(
            "RUURLUUL".to_string(),
            vec![U3, R, U, R3, U, R, U3, R3, U, R, U2, R3],
        );
        // h_rows
        algos.insert(
            "LUURRUUL".to_string(),
            vec![F, R, U, R3, U3, R, U, R3, U3, R, U, R3, U3, F3],
        );
        // h_column
        algos.insert(
            "FUULLUUB".to_string(),
            vec![U3, R, U2, R2, F, R, F3, U2, R3, F, R, F3],
        );
        // h_row
        algos.insert(
            "LUURBUUB".to_string(),
            vec![U2, Rw, U3, Rw2, D3, Rw, U3, Rw3, D, Rw2, U, Rw3],
        );
        // pi_right_bar
        algos.insert(
            "FULUULUB".to_string(),
            vec![F, R, U, R3, U3, R, U, R3, U3, F3],
        );
        // pi_back_slash
        algos.insert(
            "BURUUFUR".to_string(),
            vec![U, F, R3, F3, R, U2, R, U3, R3, U, R, U2, R3],
        );
        // pi_x_checkerboard
        algos.insert(
            "BUFUUFUB".to_string(),
            vec![U3, R3, F, R, U, F, U3, R, U, R3, U3, F3],
        );
        // pi_forward_slash
        algos.insert(
            "FURUUFUL".to_string(),
            vec![R, U2, R3, U3, R, U, R3, U2, R3, F, R, F3],
        );
        // pi_columns
        algos.insert(
            "LURUULUR".to_string(),
            vec![U3, Rw, U3, Rw2, D3, Rw, U, Rw3, D, Rw2, U, Rw3],
        );
        // pi_left_bar
        algos.insert(
            "FURUULUF".to_string(),
            vec![U3, R3, U3, R3, F, R, F3, R, U3, R3, U2, R],
        );
        // u_forward_slash
        algos.insert(
            "LBRFLUUB".to_string(),
            vec![U2, R2, D, R3, U2, R, D3, R3, U2, R3],
        );
        // u_back_slash
        algos.insert("BRFLFUUL".to_string(), vec![R2, D3, R, U2, R3, D, R, U2, R]);
        // u_front_row
        algos.insert(
            "RFFLBUUB".to_string(),
            vec![R3, U3, R, U3, R3, U2, R2, U, R3, U, R, U2, R3],
        );
        // u_rows
        algos.insert(
            "RFFLRUUL".to_string(),
            vec![U3, F, R2, D, R3, U, R, D3, R2, U3, F3],
        );
        // u_x_checkerboard
        algos.insert(
            "FLRFBUUB".to_string(),
            vec![U2, Rw, U3, Rw3, U, Rw3, D3, Rw, U3, Rw3, D, Rw],
        );
        // u_back_row
        algos.insert("LBFLFUUB".to_string(), vec![F, R, U, R3, U3, F3]);
        // t_left_bar
        algos.insert("LBRFUBLU".to_string(), vec![U3, R, U, R3, U3, R3, F, R, F3]);
        // t_right_bar
        algos.insert("FLBRURBU".to_string(), vec![U, L3, U3, L, U, L, F3, L3, F]);
        // t_rows
        algos.insert(
            "BRRFUFBU".to_string(),
            vec![R, U2, R3, U3, R, U3, R2, U2, R, U, R3, U, R],
        );
        // t_front_row
        algos.insert(
            "LBBRUFFU".to_string(),
            vec![Rw3, U, Rw, U2, R2, F, R, F3, R],
        );
        // t_back_row
        algos.insert(
            "BRLBURLU".to_string(),
            vec![Rw3, D3, Rw, U, Rw3, D, Rw, U3, Rw, U, Rw3],
        );
        // t_columns
        algos.insert(
            "BRLBUFFU".to_string(),
            vec![U2, Rw2, D3, Rw, U, Rw3, D, Rw2, U3, Rw3, U3, Rw],
        );
        // s_left_bar
        algos.insert("BRFULUBU".to_string(), vec![R, U, R3, U, R, U2, R3]);
        // s_x_checkerboard
        algos.insert("RFBULURU".to_string(), vec![L3, U2, L, U2, L, F3, L3, F]);
        // s_forward_slash
        algos.insert("RFLURUBU".to_string(), vec![F, R3, F3, R, U2, R, U2, R3]);
        // s_columns
        algos.insert(
            "RFBURULU".to_string(),
            vec![R, U, R3, U3, R3, F, R, F3, R, U, R3, U, R, U2, R3],
        );
        // s_right_bar
        algos.insert(
            "LBLUFURU".to_string(),
            vec![U2, R, U, R3, U, R3, F, R, F3, R, U2, R3],
        );
        // s_back_slash
        algos.insert("RFRULUBU".to_string(), vec![R, U3, L3, U, R3, U3, L]);
        // as_right_bar
        algos.insert("UBRFUFUL".to_string(), vec![U3, R, U2, R3, U3, R, U3, R3]);
        // as_columns
        algos.insert(
            "ULRFUBUF".to_string(),
            vec![R2, D, R3, U, R, D3, R3, U, R3, U3, R, U3, R3],
        );
        // as_back_slash
        algos.insert(
            "URFLUBUL".to_string(),
            vec![U3, F3, Rw, U, Rw3, U2, Rw3, F2, Rw],
        );
        // as_x_checkerboard
        algos.insert("UFBRURUL".to_string(), vec![R, U2, R3, U2, R3, F, R, F3]);
        // as_forward_slash
        algos.insert("URBRUFUL".to_string(), vec![L3, U, R, U3, L, U, R3]);
        // as_left_bar
        algos.insert(
            "ULFLURUB".to_string(),
            vec![U2, R, U2, R3, F, R3, F3, R, U3, R, U3, R3],
        );
        // l_mirror
        algos.insert("RFLUBRUL".to_string(), vec![F, R, U3, R3, U3, R, U, R3, F3]);
        // l_inverse
        algos.insert("FLRULBUR".to_string(), vec![F, R3, F3, R, U, R, U3, R3]);
        // l_pure
        algos.insert(
            "RFLULBUB".to_string(),
            vec![U2, R, U, R3, U, R, U3, R3, U, R, U3, R3, U, R, U2, R3],
        );
        // l_front_commutator
        algos.insert("RFRUFLUL".to_string(), vec![R, U2, R, D, R3, U2, R, D3, R2]);
        // l_diag
        algos.insert(
            "BRBUFLUR".to_string(),
            vec![U2, R3, U3, R, U, R3, F3, R, U, R3, U3, R3, F, R2],
        );
        // l_back_commutator
        algos.insert(
            "BRLULBUR".to_string(),
            vec![U3, R3, U2, R3, D3, R, U2, R3, D, R2],
        );
        Self { cube, algos }
    }

    /// Recognise which is Cube's CMLL case.
    fn recognise(&self) -> [Color; 8] {
        let mut idx = [Color::U; 8];
        for i in 0..4 {
            let cp = self.cube.cp[i];
            let co = self.cube.co[i];
            let colors = get_colors(cp, co);
            idx[i * 2] = colors.0;
            idx[i * 2 + 1] = colors.1;
        }
        idx
    }

    /// Solve the CMLL. Returns an `Vec<Move>`.
    pub fn solve(&mut self) -> Vec<Move> {
        let mut result = Vec::new();
        for i in 0..4 {
            let mut i_put = match i {
                1 => Formula {
                    moves: vec![Move::U],
                },
                2 => Formula {
                    moves: vec![Move::U2],
                },
                3 => Formula {
                    moves: vec![Move::U3],
                },
                _ => Formula { moves: vec![] },
            };
            self.cube = self.cube.apply_formula(&i_put);
            let case = self.recognise();
            for r in 0..4 {
                let case: String = case
                    .iter()
                    .map(|c| rotate_color(*c, r))
                    .map(|c| format!("{:?}", c))
                    .collect();
                let algo = self.algos.get(&case);
                if algo.is_some() {
                    let algo = Formula {
                        moves: algo.unwrap().clone(),
                    };
                    for j in 0..4 {
                        let mut j_put = match j {
                            1 => Formula {
                                moves: vec![Move::U],
                            },
                            2 => Formula {
                                moves: vec![Move::U2],
                            },
                            3 => Formula {
                                moves: vec![Move::U3],
                            },
                            _ => Formula { moves: vec![] },
                        };
                        self.cube = self.cube.apply_formula(&j_put);
                        self.cube = self.cube.apply_formula(&algo);
                        for k in 0..4 {
                            let mut k_put = match k {
                                1 => Formula {
                                    moves: vec![Move::U],
                                },
                                2 => Formula {
                                    moves: vec![Move::U2],
                                },
                                3 => Formula {
                                    moves: vec![Move::U3],
                                },
                                _ => Formula { moves: vec![] },
                            };
                            self.cube = self.cube.apply_formula(&k_put);
                            if self.is_solved() {
                                result.append(&mut i_put.moves);
                                result.append(&mut j_put.moves);
                                result.append(&mut self.algos[&case].clone());
                                result.append(&mut k_put.moves);
                                return result;
                            }
                            self.cube = self.cube.apply_formula(&k_put.inverse());
                        }
                        self.cube = self.cube.apply_formula(&algo.inverse());
                        self.cube = self.cube.apply_formula(&j_put.inverse());
                    }
                }
            }
            self.cube = self.cube.apply_formula(&i_put.inverse());
        }
        result
    }

    /// Check if Cube is solved.
    pub fn is_solved(&self) -> bool {
        let mut solved = 0;
        for i in 0..4 {
            match (i, self.cube.cp[i], self.cube.co[i]) {
                (0, Corner::URF, 0)
                | (1, Corner::UFL, 0)
                | (2, Corner::ULB, 0)
                | (3, Corner::UBR, 0) => solved += 1,
                _ => {}
            }
        }
        solved == 4
    }
}

/// get two colors at RFLB faces of a corner.
fn get_colors(cp: Corner, co: u8) -> (Color, Color) {
    let color = corner_to_face(cp);
    match co {
        0 => (color.1, color.2),
        1 => (color.0, color.1),
        _ => (color.2, color.0),
    }
}

/// rotate color for cover all CMLL cases.
fn rotate_color(color: Color, r: usize) -> Color {
    match color {
        Color::L => match r {
            1 => Color::F,
            2 => Color::R,
            3 => Color::B,
            _ => Color::L,
        },
        Color::F => match r {
            1 => Color::R,
            2 => Color::B,
            3 => Color::L,
            _ => Color::F,
        },
        Color::R => match r {
            1 => Color::B,
            2 => Color::L,
            3 => Color::F,
            _ => Color::R,
        },
        Color::B => match r {
            1 => Color::L,
            2 => Color::F,
            3 => Color::R,
            _ => Color::B,
        },
        _ => color,
    }
}

/// Split Corner expression (ex URF) to three faces(ex U, R, F).  
fn corner_to_face(corner: Corner) -> (Color, Color, Color) {
    let corner = format!("{:?}", corner);
    let corner = corner.as_bytes();
    (
        Color::try_from(char::from(corner[0])).unwrap(),
        Color::try_from(char::from(corner[1])).unwrap(),
        Color::try_from(char::from(corner[2])).unwrap(),
    )
}

#[cfg(test)]
mod tests {
    use super::super::fb::FBSolver;
    use super::super::sb::SBSolver;
    use super::CMLLSolver;
    use crate::{cubie::CubieCube, moves::Formula, solver::roux::SolverBase};

    #[test]
    fn test_cmll() {
        let cc = CubieCube::default();
        let f = Formula::scramble();
        let cc = cc.apply_formula(&f);
        let mut fb = FBSolver::new(cc);
        let _fb = fb.solve();
        assert!(fb.is_solved());
        let mut sb = SBSolver::new(fb.cube);
        let _sb = sb.solve();
        assert!(sb.is_solved());
        let mut cmll = CMLLSolver::new(sb.cube);
        let _cmll = cmll.solve();
        assert!(cmll.is_solved());
        println!(
            "Scramble: {:?}\nFirst Block: {:?}\nSecond Block: {:?}\nCMLL: {:?}",
            f.moves, _fb, _sb, _cmll
        );
    }
}