slidy 0.3.2

Utilities for working with sliding puzzles
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
//! Defines the [`Move`] type.

use std::{cmp::Ordering, fmt::Display, num::ParseIntError, ops::Add, str::FromStr};

use thiserror::Error;

use crate::algorithm::{
    as_slice::AsAlgorithmSlice,
    direction::{Direction, ParseDirectionError},
    display::r#move::{DisplayLongSpaced, DisplayLongUnspaced, DisplayShort, MoveDisplay as _},
    slice::AlgorithmSlice,
};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// A (possibly multi-tile) move of a puzzle. Contains a direction and an amount.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Move {
    pub(crate) direction: Direction,
    pub(crate) amount: u64,
}

/// Represents the sum of two moves.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum MoveSum {
    /// The sum of two moves that are in the same or opposite directions is another move.
    Ok(Move),

    /// If two moves are not in the same or opposite directions, they can not be added.
    Invalid,
}

/// Error type for [`Move`].
#[derive(Clone, Debug, Error, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum MoveError {
    /// Returned when the amount 0 is passed to [`Move::new_nonzero`].
    #[error("ZeroAmount: move amount must be greater than 0")]
    ZeroAmount,
}

impl Move {
    /// Creates a new [`Move`].
    #[must_use]
    pub fn new(direction: Direction, amount: u64) -> Self {
        Self { direction, amount }
    }

    /// Creates a new [`Move`] with the requirement that `amount` must be non-zero.
    pub fn new_nonzero(direction: Direction, amount: u64) -> Result<Self, MoveError> {
        if amount == 0 {
            Err(MoveError::ZeroAmount)
        } else {
            Ok(Self { direction, amount })
        }
    }

    /// Returns the direction of the move.
    #[must_use]
    pub fn direction(&self) -> Direction {
        self.direction
    }

    /// Returns the number of pieces moved.
    #[must_use]
    pub fn amount(&self) -> u64 {
        self.amount
    }

    /// Returns the inverse of a move. This is given by taking the inverse of the direction and
    /// leaving the amount unchanged.
    #[must_use]
    pub fn inverse(&self) -> Self {
        Self {
            direction: self.direction.inverse(),
            amount: self.amount,
        }
    }

    /// Returns the transpose of a move. This is given by taking the transpose of the direction and
    /// leaving the amount unchanged.
    #[must_use]
    pub fn transpose(&self) -> Self {
        Self {
            direction: self.direction.transpose(),
            amount: self.amount,
        }
    }

    /// Helper function for creating a [`DisplayLongSpaced`] around `self`.
    #[must_use]
    pub fn display_long_spaced(&self) -> DisplayLongSpaced {
        DisplayLongSpaced::new(*self)
    }

    /// Helper function for creating a [`DisplayLongUnspaced`] around `self`.
    #[must_use]
    pub fn display_long_unspaced(&self) -> DisplayLongUnspaced {
        DisplayLongUnspaced::new(*self)
    }

    /// Helper function for creating a [`DisplayShort`] around `self`.
    #[must_use]
    pub fn display_short(&self) -> DisplayShort {
        DisplayShort::new(*self)
    }
}

impl Display for Move {
    /// Uses [`Move::display_short`] as the default formatting.
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.display_short().fmt(f)
    }
}

/// Error type for [`Move::from_str`].
#[derive(Clone, Debug, Error, PartialEq, Eq)]
pub enum ParseMoveError {
    /// Failed to parse the direction.
    #[error("ParseDirectionError: {0}")]
    ParseDirectionError(ParseDirectionError),

    /// Failed to parse the amount.
    #[error("ParseIntError: {0}")]
    ParseIntError(ParseIntError),

    /// The string is empty.
    #[error("Empty: input string is empty")]
    Empty,
}

impl FromStr for Move {
    type Err = ParseMoveError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let mut chars = s.chars();

        let direction = chars
            .next()
            .ok_or(Self::Err::Empty)?
            .try_into()
            .map_err(Self::Err::ParseDirectionError)?;

        let rest = chars.as_str();
        let amount = if rest.is_empty() {
            1
        } else {
            rest.parse().map_err(Self::Err::ParseIntError)?
        };

        Ok(Self::new(direction, amount))
    }
}

impl From<Direction> for Move {
    /// Creates a [`Move`] from a [`Direction`] with `amount = 1`.
    fn from(direction: Direction) -> Self {
        Self {
            direction,
            amount: 1,
        }
    }
}

impl Add for Move {
    type Output = MoveSum;

    fn add(self, rhs: Self) -> Self::Output {
        if self.direction == rhs.direction {
            MoveSum::Ok(Self {
                direction: self.direction,
                amount: self.amount + rhs.amount,
            })
        } else if self.direction == rhs.direction.inverse() {
            match self.amount.cmp(&rhs.amount) {
                Ordering::Less => MoveSum::Ok(Self {
                    direction: rhs.direction,
                    amount: rhs.amount - self.amount,
                }),
                Ordering::Equal | Ordering::Greater => MoveSum::Ok(Self {
                    direction: self.direction,
                    amount: self.amount - rhs.amount,
                }),
            }
        }
        // Even if the directions are not on the same axis, we can still add the moves if one of
        // them has amount == 0 (in which case the sum is just the other move).
        // Put the check for rhs.amount == 0 first so that if they are both 0, we return self
        // instead of rhs.
        else if rhs.amount == 0 {
            MoveSum::Ok(self)
        } else if self.amount == 0 {
            MoveSum::Ok(rhs)
        } else {
            MoveSum::Invalid
        }
    }
}

impl AsAlgorithmSlice<'_> for Move {
    fn as_slice(&self) -> AlgorithmSlice<'_> {
        AlgorithmSlice {
            first: Some(*self),
            middle: &[],
            last: None,
        }
    }
}

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

    #[test]
    fn test_new() {
        let a = Move::new(Direction::Up, 2);
        assert_eq!(
            a,
            Move {
                direction: Direction::Up,
                amount: 2
            }
        );
    }

    #[test]
    fn test_new_2() {
        let a = Move::new(Direction::Up, 0);
        assert_eq!(
            a,
            Move {
                direction: Direction::Up,
                amount: 0
            }
        );
    }

    #[test]
    fn test_new_nonzero() {
        let a = Move::new_nonzero(Direction::Up, 2);
        assert_eq!(
            a,
            Ok(Move {
                direction: Direction::Up,
                amount: 2
            })
        );
    }

    #[test]
    fn test_new_nonzero_2() {
        let a = Move::new_nonzero(Direction::Up, 0);
        assert_eq!(a, Err(MoveError::ZeroAmount));
    }

    #[test]
    fn test_inverse() {
        let a = Move::new(Direction::Up, 3);
        let b = Move::new(Direction::Down, 3);
        assert_eq!(a.inverse(), b);
    }

    #[test]
    fn test_transpose() {
        let a = Move::new(Direction::Up, 3);
        let b = Move::new(Direction::Left, 3);
        assert_eq!(a.transpose(), b);
    }

    mod from_direction {
        use super::*;

        #[test]
        fn test_from_direction() {
            assert_eq!(Move::from(Direction::Up), Move::new(Direction::Up, 1));
        }
    }

    mod add {
        use super::*;

        #[test]
        fn test_add() {
            let m1 = Move::new(Direction::Up, 3);
            let m2 = Move::new(Direction::Up, 4);
            assert_eq!(
                m1 + m2,
                MoveSum::Ok(Move {
                    direction: Direction::Up,
                    amount: 7
                })
            );
        }

        #[test]
        fn test_add_2() {
            let m1 = Move {
                direction: Direction::Up,
                amount: 3,
            };
            let m2 = Move {
                direction: Direction::Down,
                amount: 4,
            };
            assert_eq!(
                m1 + m2,
                MoveSum::Ok(Move {
                    direction: Direction::Down,
                    amount: 1
                })
            );
        }

        #[test]
        fn test_add_3() {
            let m1 = Move {
                direction: Direction::Left,
                amount: 5,
            };
            let m2 = Move {
                direction: Direction::Right,
                amount: 5,
            };
            assert_eq!(
                m1 + m2,
                MoveSum::Ok(Move {
                    direction: Direction::Left,
                    amount: 0
                })
            );
        }

        #[test]
        fn test_add_4() {
            let m1 = Move {
                direction: Direction::Left,
                amount: 2,
            };
            let m2 = Move {
                direction: Direction::Up,
                amount: 1,
            };
            assert_eq!(m1 + m2, MoveSum::Invalid);
        }

        #[test]
        fn test_add_5() {
            let m1 = Move {
                direction: Direction::Up,
                amount: 0,
            };
            let m2 = Move {
                direction: Direction::Down,
                amount: 2,
            };
            assert_eq!(m1 + m2, MoveSum::Ok(m2));
            assert_eq!(m2 + m1, MoveSum::Ok(m2));
        }

        #[test]
        fn test_add_6() {
            let m1 = Move {
                direction: Direction::Up,
                amount: 0,
            };
            let m2 = Move {
                direction: Direction::Left,
                amount: 2,
            };
            assert_eq!(m1 + m2, MoveSum::Ok(m2));
            assert_eq!(m2 + m1, MoveSum::Ok(m2));
        }

        #[test]
        fn test_add_7() {
            let m1 = Move {
                direction: Direction::Up,
                amount: 0,
            };
            let m2 = Move {
                direction: Direction::Left,
                amount: 0,
            };
            assert_eq!(m1 + m2, MoveSum::Ok(m1));
            assert_eq!(m2 + m1, MoveSum::Ok(m2));
        }
    }
}

#[cfg(all(feature = "nightly", test))]
mod benchmarks {
    extern crate test;

    use test::Bencher;

    use super::*;

    #[bench]
    fn bench_display_long_spaced(b: &mut Bencher) {
        let m = Move::new(Direction::Up, 10);
        b.iter(|| {
            for _ in 0..100 {
                DisplayLongSpaced::new(m).to_string();
            }
        });
    }

    #[bench]
    fn bench_display_long_unspaced(b: &mut Bencher) {
        let m = Move::new(Direction::Up, 10);
        b.iter(|| {
            for _ in 0..100 {
                DisplayLongUnspaced::new(m).to_string();
            }
        });
    }

    #[bench]
    fn bench_display_short(b: &mut Bencher) {
        let m = Move::new(Direction::Up, 10);
        b.iter(|| {
            for _ in 0..100 {
                DisplayShort::new(m).to_string();
            }
        });
    }
}