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
use num_integer::binomial;
use std::convert::{TryFrom, TryInto};

/// Encode the position of the interesting elements in a slice as a single natural number.
/// Note that we don't care about the relative positions of the interesting elements, which allows
/// us to deal with a smaller number of possibilities.
///
/// In the context of a Rubiks Cube, the calculation is explained with an example here:
/// http://kociemba.org/math/UDSliceCoord.htm
pub fn encode_position<T: Ord, Encoded: TryFrom<usize>>(
    data: &[T],
    is_interesting: &dyn Fn(&T) -> bool,
) -> Result<Encoded, Encoded::Error> {
    let mut interesting_to_the_left = 0;
    data.iter()
        .enumerate()
        .map(|(index, x)| {
            if is_interesting(x) {
                interesting_to_the_left += 1;
            }
            (index, x, interesting_to_the_left)
        })
        .filter_map(|(index, x, interesting_to_the_left)| {
            if is_interesting(x) || interesting_to_the_left == 0 {
                None
            } else {
                Some((index, interesting_to_the_left))
            }
        })
        .map(|(index, interesting_to_the_left)| binomial(index, interesting_to_the_left - 1))
        .sum::<usize>()
        .try_into()
}

/// Decode the position number of a slice
/// Returns a Vec<bool> filled with false for all uninteresting elements and true for all
/// interesting elements.
/// This Vec can be used as a mapping of indices to interesting elements.
pub fn decode_position<ToDecode: TryInto<usize>>(
    position: ToDecode,
    num_interesting: usize,
    len: usize,
) -> Result<Vec<bool>, ToDecode::Error> {
    let mut interesting_to_the_left = num_interesting;
    let mut position = position.try_into()?;
    let mut result = (0..len)
        .rev()
        .map(|index| {
            let cutoff = binomial(index, interesting_to_the_left - 1).into();
            if position < cutoff {
                interesting_to_the_left -= 1;
                true
            } else {
                position -= cutoff;
                false
            }
        })
        .collect::<Vec<_>>();
    result.reverse();
    Ok(result)
}

#[cfg(test)]
mod tests {
    use super::*;
    fn is_interesting(x: &u8) -> bool {
        *x != 0
    }
    fn examples() -> Vec<([u8; 12], usize)> {
        vec![
            ([0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1], 0),
            ([0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1], 1),
            ([0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1], 2),
            ([1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1], 8),
            ([0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1], 9),
            ([0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1], 62),
            ([1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1], 164),
            ([0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0], 165),
            ([0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0], 305),
            ([1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0], 494),
        ]
    }
    #[test]
    fn test_encode_position() {
        for (data, position) in examples().iter() {
            assert_eq!(Ok(*position), encode_position(data, &is_interesting));
        }
    }
    #[test]
    fn test_encode_position_fails_with_too_small_type() {
        assert!(
            encode_position::<_, u8>(&[1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0], &is_interesting)
                .is_err()
        )
    }
    #[test]
    fn test_decode_position() {
        for (data, position) in examples().iter() {
            let expected = data.iter().map(is_interesting).collect::<Vec<_>>();
            let num_interesting = expected.iter().filter(|x| **x).count();
            assert_eq!(
                Ok(expected),
                decode_position(*position, num_interesting, data.len())
            );
        }
    }
}