three_style_lib/moves/
core.rs

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
use crate::error::Error;
use std::{fmt, ops::Mul, str::FromStr};

pub trait Inverse {
    fn inverse(&self) -> Self;
}

#[rustfmt::skip]
#[derive(Debug, PartialEq, Eq, Clone, Copy, PartialOrd, Ord, Hash)]
pub enum MoveKind {
    U, R, F, D, L, B,
    X, Y, Z, M, E, S,
    Uw, Rw, Fw, Dw, Lw, Bw,
}

impl MoveKind {
    pub fn is_side(self) -> bool {
        matches!(
            self,
            Self::U | Self::R | Self::F | Self::D | Self::L | Self::B
        )
    }

    pub fn is_rotation(self) -> bool {
        matches!(self, Self::X | Self::Y | Self::Z)
    }

    pub fn is_slice(self) -> bool {
        matches!(self, Self::M | Self::E | Self::S)
    }

    pub fn is_wide(self) -> bool {
        matches!(
            self,
            Self::Uw | Self::Rw | Self::Fw | Self::Dw | Self::Lw | Self::Bw
        )
    }

    pub fn to_moves(&self) -> [Move; 3] {
        [
            Move::new(*self, MoveCount::Simple),
            Move::new(*self, MoveCount::Double),
            Move::new(*self, MoveCount::Prime),
        ]
    }

    pub fn parallel(&self) -> Vec<MoveKind> {
        match self {
            MoveKind::E => vec![MoveKind::U, MoveKind::D],
            MoveKind::M => vec![MoveKind::R, MoveKind::L],
            MoveKind::S => vec![MoveKind::F, MoveKind::B],
            MoveKind::U | MoveKind::D => vec![self.inverse(), MoveKind::E],
            MoveKind::R | MoveKind::L => vec![self.inverse(), MoveKind::M],
            MoveKind::F | MoveKind::B => vec![self.inverse(), MoveKind::S],
            _ => vec![self.inverse()],
        }
    }
}

impl Inverse for MoveKind {
    fn inverse(&self) -> Self {
        match self {
            MoveKind::U => MoveKind::D,
            MoveKind::R => MoveKind::L,
            MoveKind::F => MoveKind::B,
            MoveKind::D => MoveKind::U,
            MoveKind::L => MoveKind::R,
            MoveKind::B => MoveKind::F,
            MoveKind::Uw => MoveKind::Dw,
            MoveKind::Rw => MoveKind::Lw,
            MoveKind::Fw => MoveKind::Bw,
            MoveKind::Dw => MoveKind::Uw,
            MoveKind::Lw => MoveKind::Rw,
            MoveKind::Bw => MoveKind::Fw,
            _ => *self,
        }
    }
}

impl FromStr for MoveKind {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "U" => Ok(MoveKind::U),
            "F" => Ok(MoveKind::F),
            "R" => Ok(MoveKind::R),
            "B" => Ok(MoveKind::B),
            "L" => Ok(MoveKind::L),
            "D" => Ok(MoveKind::D),
            "M" => Ok(MoveKind::M),
            "S" => Ok(MoveKind::S),
            "E" => Ok(MoveKind::E),
            "x" => Ok(MoveKind::X),
            "y" => Ok(MoveKind::Y),
            "z" => Ok(MoveKind::Z),
            "u" => Ok(MoveKind::Uw),
            "f" => Ok(MoveKind::Fw),
            "r" => Ok(MoveKind::Rw),
            "b" => Ok(MoveKind::Bw),
            "l" => Ok(MoveKind::Lw),
            "d" => Ok(MoveKind::Dw),
            _ => Err(Error::InvalidMove(s.to_owned())),
        }
    }
}

impl fmt::Display for MoveKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:?}", self)
    }
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub enum MoveCount {
    Simple = 1,
    Double = 2,
    Prime = 3,
}

impl Inverse for MoveCount {
    fn inverse(&self) -> Self {
        match self {
            MoveCount::Simple => MoveCount::Prime,
            MoveCount::Double => MoveCount::Double,
            MoveCount::Prime => MoveCount::Simple,
        }
    }
}

impl FromStr for MoveCount {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "2" => Ok(MoveCount::Double),
            "'" => Ok(MoveCount::Prime),
            "" => Ok(MoveCount::Simple),
            _ => Err(Error::InvalidMove(s.to_owned())),
        }
    }
}

impl fmt::Display for MoveCount {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            MoveCount::Simple => write!(f, ""),
            MoveCount::Double => write!(f, "2"),
            MoveCount::Prime => write!(f, "'"),
        }
    }
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub struct Move {
    pub kind: MoveKind,
    pub count: MoveCount,
}

impl Move {
    pub fn new(kind: MoveKind, count: MoveCount) -> Self {
        Self { kind, count }
    }

    fn reduce(&self, rhs: Move) -> Option<Move> {
        use {MoveCount as C, MoveKind as M};

        let is_inversed = self.count.inverse() == rhs.count;
        let is_slice = self.kind.is_slice();
        let count = if is_slice { rhs.count } else { self.count }; // determined by wide or side moves

        match (self.kind, self.count, rhs.kind, rhs.count) {
            // wide move generators
            (M::U, _, M::E, _) => Some(Move::new(M::Uw, count)),
            (M::E, _, M::D, _) if is_inversed => Some(Move::new(M::Dw, count)),
            (M::L, _, M::M, _) => Some(Move::new(M::Lw, count)),
            (M::M, _, M::R, _) if is_inversed => Some(Move::new(M::Rw, count)),
            (M::F, _, M::S, _) => Some(Move::new(M::Fw, count)),
            (M::S, _, M::B, _) if is_inversed => Some(Move::new(M::Bw, count)),

            // wide move reduction
            (M::R, C::Prime, M::Rw, C::Simple) => Some(Move::new(M::M, C::Prime)),
            (M::R, C::Simple, M::Rw, C::Prime) => Some(Move::new(M::M, C::Simple)),
            (M::L, C::Prime, M::Lw, C::Simple) => Some(Move::new(M::M, C::Simple)),
            (M::L, C::Simple, M::Lw, C::Prime) => Some(Move::new(M::M, C::Prime)),
            (M::U, C::Prime, M::Uw, C::Simple) => Some(Move::new(M::E, C::Simple)),
            (M::U, C::Simple, M::Uw, C::Prime) => Some(Move::new(M::E, C::Prime)),
            (M::D, C::Prime, M::Dw, C::Simple) => Some(Move::new(M::E, C::Prime)),
            (M::D, C::Simple, M::Dw, C::Prime) => Some(Move::new(M::E, C::Simple)),
            (M::F, C::Prime, M::Fw, C::Simple) => Some(Move::new(M::S, C::Simple)),
            (M::F, C::Simple, M::Fw, C::Prime) => Some(Move::new(M::S, C::Prime)),
            (M::B, C::Prime, M::Bw, C::Simple) => Some(Move::new(M::S, C::Prime)),
            (M::B, C::Simple, M::Bw, C::Prime) => Some(Move::new(M::S, C::Simple)),

            (M::M, _, M::Lw, _) if is_inversed => Some(Move::new(M::L, count)),
            (M::M, _, M::Rw, _) if self.count == rhs.count => Some(Move::new(M::R, count)),
            (M::E, _, M::Uw, _) if is_inversed => Some(Move::new(M::U, count)),
            (M::E, _, M::Dw, _) if self.count == rhs.count => Some(Move::new(M::D, count)),
            (M::S, _, M::Fw, _) if is_inversed => Some(Move::new(M::F, count)),
            (M::S, _, M::Bw, _) if self.count == rhs.count => Some(Move::new(M::B, count)),

            // move count reduction
            (_, _, _, _) if self.kind == rhs.kind && !is_inversed => {
                let kind = self.kind;
                let count = match (self.count as usize + rhs.count as usize) % 4 {
                    2 => MoveCount::Double,
                    3 => MoveCount::Prime,
                    _ => MoveCount::Simple,
                };
                Some(Move { kind, count })
            }
            _ => None,
        }
    }
}

impl Inverse for Move {
    fn inverse(&self) -> Self {
        Self::new(self.kind, self.count.inverse())
    }
}

impl Mul<Move> for Move {
    type Output = Option<Move>;

    fn mul(self, rhs: Move) -> Self::Output {
        self.reduce(rhs).or(rhs.reduce(self))
    }
}

impl FromStr for Move {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let kind = s.get(..1).unwrap_or_default();
        let kind = MoveKind::from_str(kind)?;
        let count = s.get(1..2).unwrap_or_default();
        let count = MoveCount::from_str(count)?;

        Ok(Self { kind, count })
    }
}

impl fmt::Display for Move {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}{}", self.kind, self.count)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_move_str() {
        assert_eq!(
            Move::from_str("R"),
            Ok(Move::new(MoveKind::R, MoveCount::Simple))
        );
        assert_eq!(
            Move::from_str("R2"),
            Ok(Move::new(MoveKind::R, MoveCount::Double))
        );
        assert_eq!(
            Move::from_str("R'"),
            Ok(Move::new(MoveKind::R, MoveCount::Prime))
        );
    }
}