Simplify
This commit is contained in:
parent
5a69105ec3
commit
a66e590b35
1 changed files with 5 additions and 9 deletions
|
|
@ -80,7 +80,7 @@ impl Decrypter {
|
||||||
// i | [x, y, z, a, b, c]
|
// i | [x, y, z, a, b, c]
|
||||||
assert_eq!(i, m);
|
assert_eq!(i, m);
|
||||||
|
|
||||||
let n = delta(n, self.ilen);
|
let n = imod(n, self.ptr.len());
|
||||||
self.ptr.rotate_left(n);
|
self.ptr.rotate_left(n);
|
||||||
self.ptr.push_front(m);
|
self.ptr.push_front(m);
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -92,18 +92,14 @@ impl Decrypter {
|
||||||
// [x, y, z, a, b, c] | i
|
// [x, y, z, a, b, c] | i
|
||||||
assert_eq!(i, m);
|
assert_eq!(i, m);
|
||||||
|
|
||||||
let n = delta(n, self.ilen);
|
let n = imod(n, self.ptr.len());
|
||||||
self.ptr.rotate_right(n);
|
self.ptr.rotate_right(n);
|
||||||
self.ptr.push_back(m);
|
self.ptr.push_back(m);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn delta(n: i64, len: i64) -> usize {
|
fn imod(n: i64, m: usize) -> usize {
|
||||||
let n = n.abs();
|
let n: usize = n.abs().try_into().unwrap();
|
||||||
// It takes n-1 swaps to get back to where we started, so treat these as extra "hops" over the
|
n % m
|
||||||
// original position.
|
|
||||||
let skips = n / (len - 1);
|
|
||||||
let extra = n % len;
|
|
||||||
((skips + extra) % len).try_into().unwrap()
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue