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
//! Defines the [`AlgorithmSlice`] type.
use std::{
fmt::Display,
iter::{self, Sum},
};
use num_traits::{AsPrimitive, PrimInt};
use crate::{
algorithm::{
algorithm::Algorithm,
direction::Direction,
display::{
algorithm::{AlgorithmDisplay as _, DisplaySpaced, DisplayUnspaced},
r#move::{DisplayLongSpaced, DisplayLongUnspaced, DisplayShort},
},
metric::{Metric, Mtm, Stm},
moves::Moves,
r#move::r#move::{Move, MoveSum},
},
puzzle::size::Size,
};
/// A slice of an [`Algorithm`].
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct AlgorithmSlice<'a> {
// We might slice in the middle of one move, e.g. D10LU10R[5..15] should be D5LU4. To represent
// this, we need to store the first and last moves separately.
pub(super) first: Option<Move>,
pub(super) middle: &'a [Move],
pub(super) last: Option<Move>,
}
impl AlgorithmSlice<'_> {
/// The length of the slice in the [`Metric`] `M`.
#[must_use]
pub fn len<M: Metric, T: PrimInt + Sum + 'static>(&self) -> T
where
u64: AsPrimitive<T>,
{
self.moves().map(|m| M::len::<T>(m)).sum()
}
/// The length of the slice in the [`Stm`] [`Metric`].
#[must_use]
pub fn len_stm<T: PrimInt + Sum + 'static>(&self) -> T
where
u64: AsPrimitive<T>,
{
self.len::<Stm, T>()
}
/// The length of the slice in the [`Mtm`] [`Metric`].
#[must_use]
pub fn len_mtm<T: PrimInt + Sum + 'static>(&self) -> T
where
u64: AsPrimitive<T>,
{
self.len::<Mtm, T>()
}
/// Checks if the slice is empty.
#[must_use]
pub fn is_empty(&self) -> bool {
self.first.is_none() && self.middle.is_empty() && self.last.is_none()
}
/// Combines all consecutive moves along the same axis into a single move, and removes any moves
/// that cancel completely. Returns the result as a new [`Algorithm`].
#[must_use]
pub fn simplified(&self) -> Algorithm {
if self.moves().count() < 2 {
return Algorithm::from(*self);
}
// List of simplified moves
let mut moves = Vec::new();
// Current move that we are accumulating into. This will be pushed to `moves` when we
// reach a move that can't be added to it.
let mut acc_move = None;
for next in self.moves() {
match acc_move {
Some(sum) => match sum + next {
MoveSum::Ok(m) => {
// Moves completely cancel.
acc_move = if m.amount == 0 {
// Try and pop a move off `moves`, because the next move might cancel.
// e.g. consider URLD where `next` is the L move. We pop the U move
// from `moves` so that the following D move can cancel with it.
moves.pop()
}
// Moves can be added but don't fully cancel, keep accumulating into mv.
else {
Some(m)
};
}
// Moves can't be added, there is no more simplification at this point.
MoveSum::Invalid => {
// Push mv and go to the next move.
moves.push(sum);
acc_move = Some(next);
}
},
None => acc_move = Some(next),
}
}
if let Some(m) = acc_move {
if m.amount != 0 {
moves.push(m);
}
}
Algorithm::with_moves(moves)
}
/// Returns the result of reflecting the algorithm through the main diagonal as a new
/// [`Algorithm`].
#[must_use]
pub fn transpose(&self) -> Algorithm {
Algorithm::with_moves(self.moves().map(|m| m.transpose()).collect())
}
/// Concatenates `n` copies of `self` and returns the result as a new [`Algorithm`].
#[must_use]
pub fn repeat(&self, n: usize) -> Algorithm {
let len = self.moves().len();
Algorithm::with_moves(self.moves().cycle().take(len * n).collect::<Vec<_>>())
}
/// Returns `Some(size)` if `self` can be applied to a solved puzzle (with the gap in the
/// bottom right) of size `size` but cannot be applied to any smaller solved puzzle. Returns
/// `None` if `self` can not be applied to a puzzle of any size.
#[must_use]
pub fn min_applicable_size(&self) -> Option<Size> {
// Gap position where (0, 0) is the bottom right position, increasing up and left
let (mut max_gx, mut max_gy) = (0u64, 0u64);
let (mut gx, mut gy) = (0u64, 0u64);
for mv in self.moves() {
let n = mv.amount;
// Update the gap position occurs and return `None` if overflow/underflow occurs
match mv.direction {
Direction::Up => gy = gy.checked_sub(n)?,
Direction::Left => gx = gx.checked_sub(n)?,
Direction::Down => gy = gy.checked_add(n)?,
Direction::Right => gx = gx.checked_add(n)?,
}
max_gx = max_gx.max(gx);
max_gy = max_gy.max(gy);
}
Size::new(1 + max_gx, 1 + max_gy).ok()
}
/// An iterator over the single-tile moves in the slice.
///
/// # Example
///
/// ```
/// # use std::str::FromStr as _;
/// # use slidy::algorithm::{
/// # algorithm::Algorithm, as_slice::AsAlgorithmSlice, direction::Direction, r#move::r#move::Move,
/// # };
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let alg = Algorithm::from_str("RD3LUR2")?;
/// let slice = alg.as_slice();
///
/// let mut iter = slice.single_tile_moves();
/// assert_eq!(iter.next(), Some(Direction::Right));
/// assert_eq!(iter.next(), Some(Direction::Down));
/// assert_eq!(iter.next(), Some(Direction::Down));
/// assert_eq!(iter.next(), Some(Direction::Down));
/// assert_eq!(iter.next(), Some(Direction::Left));
/// assert_eq!(iter.next(), Some(Direction::Up));
/// assert_eq!(iter.next(), Some(Direction::Right));
/// assert_eq!(iter.next(), Some(Direction::Right));
/// assert_eq!(iter.next(), None);
/// # Ok(())
/// # }
/// ```
pub fn single_tile_moves(&self) -> impl Iterator<Item = Direction> + '_ {
self.moves()
.flat_map(|m| iter::repeat_n(m.direction, m.amount as usize))
}
/// An iterator over the multi-tile moves in the slice.
///
/// # Example
///
/// ```
/// # use std::str::FromStr as _;
/// # use slidy::algorithm::{
/// # algorithm::Algorithm, as_slice::AsAlgorithmSlice, direction::Direction, r#move::r#move::Move,
/// # };
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let alg = Algorithm::from_str("RD3LUR2")?;
/// let slice = alg.as_slice();
///
/// let mut iter = slice.moves();
/// assert_eq!(iter.next(), Some(Move::new(Direction::Right, 1)));
/// assert_eq!(iter.next(), Some(Move::new(Direction::Down, 3)));
/// assert_eq!(iter.next(), Some(Move::new(Direction::Left, 1)));
/// assert_eq!(iter.next(), Some(Move::new(Direction::Up, 1)));
/// assert_eq!(iter.next(), Some(Move::new(Direction::Right, 2)));
/// assert_eq!(iter.next(), None);
/// # Ok(())
/// # }
/// ```
#[must_use]
pub fn moves(&self) -> Moves<'_> {
Moves::new(*self)
}
/// Helper function for creating a [`DisplaySpaced<DisplayLongSpaced>`] around `self`.
#[must_use]
pub fn display_long_spaced(&self) -> DisplaySpaced<'_, DisplayLongSpaced> {
DisplaySpaced::<DisplayLongSpaced>::new(self)
}
/// Helper function for creating a [`DisplayUnspaced<DisplayLongUnspaced>`] around `self`.
#[must_use]
pub fn display_long_unspaced(&self) -> DisplayUnspaced<'_, DisplayLongUnspaced> {
DisplayUnspaced::<DisplayLongUnspaced>::new(self)
}
/// Helper function for creating a [`DisplaySpaced<DisplayShort>`] around `self`.
#[must_use]
pub fn display_short_spaced(&self) -> DisplaySpaced<'_, DisplayShort> {
DisplaySpaced::<DisplayShort>::new(self)
}
/// Helper function for creating a [`DisplayUnspaced<DisplayShort>`] around `self`.
#[must_use]
pub fn display_short_unspaced(&self) -> DisplayUnspaced<'_, DisplayShort> {
DisplayUnspaced::<DisplayShort>::new(self)
}
}
impl Display for AlgorithmSlice<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
// Default formatting is short, unspaced.
self.display_short_unspaced().fmt(f)
}
}
#[cfg(test)]
mod tests {
use std::str::FromStr as _;
use crate::algorithm::as_slice::AsAlgorithmSlice as _;
use super::*;
#[test]
fn test_len() -> Result<(), Box<dyn std::error::Error>> {
let alg = Algorithm::from_str("R3D2LDR5U12RD3LU4R")?;
for start in 0..34 {
for end in start..34 {
let slice = alg.try_slice(start..end)?;
assert_eq!(slice.len_stm::<u64>(), end - start);
}
}
Ok(())
}
#[test]
fn test_is_empty() -> Result<(), Box<dyn std::error::Error>> {
let alg = Algorithm::from_str("R3D2LDR5U12RD3LU4R")?;
for start in 0..34 {
for end in start..34 {
let slice = alg.try_slice(start..end)?;
assert_eq!(slice.is_empty(), (start..end).is_empty());
}
}
Ok(())
}
#[test]
fn test_min_applicable_size() {
let size = |alg: &str| -> Option<Size> {
Algorithm::from_str(alg)
.unwrap()
.as_slice()
.min_applicable_size()
};
assert_eq!(size("DR"), Size::new(2, 2).ok());
assert_eq!(size("D3RU2RD2RU3L3"), Size::new(4, 4).ok());
assert_eq!(size("D10000"), Size::new(1, 10001).ok());
assert_eq!(size("R10000"), Size::new(10001, 1).ok());
assert_eq!(size("D9R9U9L9D8R8UL7U4R3D2L"), Size::new(10, 10).ok());
assert_eq!(size("RDRDRDRDRDRDRDRDRD"), Size::new(10, 10).ok());
assert_eq!(size("RDLD2R2U2RDL2U2LDRURDLUR2D2LU"), Size::new(4, 4).ok());
assert_eq!(size("L"), None);
assert_eq!(size("U"), None);
assert_eq!(size("DRU2LDRD"), None);
assert_eq!(size("R3DL4RURDLU"), None);
}
#[test]
fn test_single_tile_moves() -> Result<(), Box<dyn std::error::Error>> {
let alg = Algorithm::from_str("R3D2LDR5U12RD3LU4R")?;
let slice = alg.try_slice(4..19)?;
let mut moves = slice.single_tile_moves();
assert_eq!(moves.next(), Some(Direction::Down));
assert_eq!(moves.next(), Some(Direction::Left));
assert_eq!(moves.next(), Some(Direction::Down));
assert_eq!(moves.next(), Some(Direction::Right));
assert_eq!(moves.next(), Some(Direction::Right));
assert_eq!(moves.next(), Some(Direction::Right));
assert_eq!(moves.next(), Some(Direction::Right));
assert_eq!(moves.next(), Some(Direction::Right));
assert_eq!(moves.next(), Some(Direction::Up));
assert_eq!(moves.next(), Some(Direction::Up));
assert_eq!(moves.next(), Some(Direction::Up));
assert_eq!(moves.next(), Some(Direction::Up));
assert_eq!(moves.next(), Some(Direction::Up));
assert_eq!(moves.next(), Some(Direction::Up));
assert_eq!(moves.next(), Some(Direction::Up));
assert_eq!(moves.next(), None);
assert_eq!(moves.next(), None);
Ok(())
}
}