diff options
Diffstat (limited to '2022/06/common.rs')
| -rw-r--r-- | 2022/06/common.rs | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/2022/06/common.rs b/2022/06/common.rs new file mode 100644 index 0000000..ed5360c --- /dev/null +++ b/2022/06/common.rs | |||
| @@ -0,0 +1,20 @@ | |||
| 1 | fn all_distinct<T: PartialEq>(a: &[T]) -> bool { | ||
| 2 | for i in 0..a.len() { | ||
| 3 | for j in i+1..a.len() { | ||
| 4 | if a[i] == a[j] { | ||
| 5 | return false; | ||
| 6 | } | ||
| 7 | } | ||
| 8 | } | ||
| 9 | return true; | ||
| 10 | } | ||
| 11 | |||
| 12 | pub fn first_index_n_distinct(a: &[u8], n: usize) -> usize { | ||
| 13 | assert!(n > 0, "{} must be greater than 0", n); | ||
| 14 | for i in 0..a.len()-n+1 { | ||
| 15 | if all_distinct(&a[i..i+n]) { | ||
| 16 | return i+n-1; | ||
| 17 | } | ||
| 18 | } | ||
| 19 | panic!("Cannot find {} distinct in a row", n); | ||
| 20 | } | ||
