summaryrefslogtreecommitdiff
path: root/src/row.rs
blob: fc0633b3c7d70f26a8f8e3e11cb025bb5bf391f3 (plain)
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
pub fn encode_row(n: u32, mut x: u64) -> Vec<u32> {
    let mut row = Vec::with_capacity(n as usize);
    let mut pieces: Vec<u32> = (0..n).collect();
    for i in (1..=n.into()).rev() {
        let p = (x % i) as u32;
        let piece = pieces[p as usize];
        row.push(piece + 1);
        pieces.swap_remove(p as usize);
        x /= i;
    }
    row
}

pub fn colliding(w1: &[u32], w2: &[u32]) -> bool {
    let n = w1.len() - 1;
    let (mut v1, mut v2) = (w1.iter().take(n), w2.iter().take(n));
    let (mut i, mut j) = (*v1.next().unwrap(), *v2.next().unwrap());
    loop {
        if i < j {
            i += if let Some(&k) = v1.next() { k } else { return false; };
        } else if i > j {
            j += if let Some(&k) = v2.next() { k } else { return false; };
        } else {
            return true;
        }
    }
}