aoc

My solutions for the Advent of Code
git clone https://git.tronto.net/aoc
Download | Log | Files | Refs | README

common.rs (493B)


      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 }