From 15a42ef20c2fdfb5fe68ea348579b32b1f48f742 Mon Sep 17 00:00:00 2001 From: Michael Peters Date: Tue, 12 Mar 2024 16:10:56 -0700 Subject: [PATCH] parse and apply moves --- src/main.rs | 97 ++++++++++++++++++++--------------------------------- 1 file changed, 37 insertions(+), 60 deletions(-) diff --git a/src/main.rs b/src/main.rs index 59c210b..163ea42 100644 --- a/src/main.rs +++ b/src/main.rs @@ -119,7 +119,6 @@ impl FromStr for Move { type Err = &'static str; fn from_str(s: &str) -> Result { - println!("\"{}\"", s); match s { "U" => Ok(Move::U), "U2" => Ok(Move::U2), @@ -144,26 +143,19 @@ impl FromStr for Move { } } -fn parse_moves(s: &str) -> Result, &'static str> { +fn parse_moves(moves_str: &str) -> Result, &'static str> { let mut moves = Vec::new(); - let mut chars = s.chars().filter(|&c| c != ' ').peekable(); + let mut chars = moves_str.chars().filter(|&c| c != ' ').peekable(); while let Some(c) = chars.next() { let nco = chars.peek(); match nco { - Some(nc) => match nc { - '2' | '\'' => { - let s = format!("{}{}", c, nc); - let m = Move::from_str(s.as_str()).expect("unable to parse move"); - moves.push(m); - chars.next(); - } - _ => { - let s = c.to_string(); - let m = Move::from_str(s.as_str()).expect("unable to parse move"); - moves.push(m); - } - }, + Some(nc) if (nc == &'2' || nc == &'\'') => { + let s = format!("{}{}", c, nc); + let m = Move::from_str(s.as_str()).expect("unable to parse move"); + moves.push(m); + chars.next(); + } _ => { let s = c.to_string(); let m = Move::from_str(s.as_str()).expect("unable to parse move"); @@ -175,6 +167,32 @@ fn parse_moves(s: &str) -> Result, &'static str> { Ok(moves) } +#[rustfmt::skip] +fn apply_moves(c: &mut Cube, moves: Vec) { + for m in moves { + match m { + Move::U => { c.u(); }, + Move::U2 => { c.u(); c.u(); }, + Move::Up => { c.u(); c.u(); c.u(); }, + Move::D => { c.d(); }, + Move::D2 => { c.d(); c.d(); }, + Move::Dp => { c.d(); c.d(); c.d(); }, + Move::L => { c.l(); }, + Move::L2 => { c.l(); c.l(); }, + Move::Lp => { c.l(); c.l(); c.l(); }, + Move::R => { c.r(); }, + Move::R2 => { c.r(); c.r(); } + Move::Rp => { c.r(); c.r(); c.r(); } + Move::F => { c.f(); }, + Move::F2 => { c.f(); c.f(); } + Move::Fp => { c.f(); c.f(); c.f(); } + Move::B => { c.b(); }, + Move::B2 => { c.b(); c.b(); } + Move::Bp => { c.b(); c.b(); c.b(); } + } + } +} + struct Cube { u: Face, d: Face, @@ -272,55 +290,14 @@ impl Cube { self.l.apply_l(u_tr, u_tm, u_tl); } - // TODO: efficient counter-clockwise - fn up(&mut self) { - self.u(); - self.u(); - self.u(); - } - - fn dp(&mut self) { - self.d(); - self.d(); - self.d(); - } - - fn lp(&mut self) { - self.l(); - self.l(); - self.l(); - } - - fn rp(&mut self) { - self.r(); - self.r(); - self.r(); - } - - fn fp(&mut self) { - self.f(); - self.f(); - self.f(); - } - - fn bp(&mut self) { - self.b(); - self.b(); - self.b(); - } + // TODO: efficient double / counter-clockwise } fn main() { let mut c = Cube::new(); println!("Here's the cube:\n{}", c); - c.r(); - c.u(); - c.rp(); - c.u(); - c.r(); - c.u(); - c.u(); - c.rp(); + let moves = parse_moves("R U R' U R U2 R'").expect("unable to parse moves"); + apply_moves(&mut c, moves); println!("after OLL:\n{}", c); }