Untitled

mail@pastecode.io avatar
unknown
rust
a year ago
613 B
6
Indexable
Never
use std::fs;

fn main() {
    let content = fs::read_to_string("input.txt").expect("failed to read file");

    println!("part 1: {}", part_one(&content, 4));
    println!("part 2: {}", part_one(&content, 14));
}

fn part_one(input: &str, window: usize) -> usize {
    input
        .as_bytes()
        .windows(window)
        .filter_map(|elements| {
            elements
                .into_iter()
                .map(|elem| 1 << (*elem - b'a'))
                .reduce(|left, right| left | right)
        })
        .position(|num: u64| num.count_ones() == window as _)
        .unwrap()
        + window
}