summaryrefslogtreecommitdiff
path: root/2022/src/bin/day_6.rs
blob: f02feca38c9da81d2442cd586fad79fda9429827 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
use std::{collections::BTreeSet, fs};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let input = fs::read_to_string("inputs/day_6.txt")?;
    let chars: Vec<char> = input.trim().chars().collect();
    dbg!(find_distinct_char_run(&chars, 4));
    dbg!(find_distinct_char_run(&chars, 14));

    Ok(())
}

fn find_distinct_char_run(chars: &[char], window_size: usize) -> Option<usize> {
    for (i, char_window) in chars.windows(window_size).enumerate() {
        let set: BTreeSet<&char> = char_window.iter().collect();
        if set.len() == window_size {
            return Some(i + window_size);
        }
    }
    return None;
}