Skip to content

Commit

Permalink
fix: check duplicated pieces (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
luckasRanarison committed May 15, 2024
1 parent 6b6f574 commit 2a403f9
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
6 changes: 5 additions & 1 deletion kewb/src/cube/cubie.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use self::{Corner::*, Edge::*, Move::*};
use crate::error::Error;

use super::{facelet::*, moves::*};
use super::{facelet::*, moves::*, utils::has_duplicates};
use std::ops::Mul;

/// Represents the 8 corners on the cube, described by the layer they are on.
Expand Down Expand Up @@ -191,6 +191,10 @@ impl CubieCube {

/// Checks if CubieCube is a valid cubie representation.
pub fn is_solvable(&self) -> bool {
if has_duplicates(&self.cp) || has_duplicates(&self.ep) {
return false;
}

let c_perm = self.count_corner_perm();
let e_perm = self.count_edge_perm();
let c_twist = self.count_corner_twist();
Expand Down
1 change: 1 addition & 0 deletions kewb/src/cube/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ pub mod generators;
pub mod index;
pub mod moves;
pub mod scramble;
mod utils;
6 changes: 6 additions & 0 deletions kewb/src/cube/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pub fn has_duplicates<T: PartialEq>(items: &[T]) -> bool {
items
.iter()
.enumerate()
.any(|(i, item)| items[i + 1..].contains(item))
}

0 comments on commit 2a403f9

Please sign in to comment.