Quest 6: Mentorship Matrix

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

Link to participate: https://everybody.codes/

  • hades@programming.devOPM
    link
    fedilink
    arrow-up
    3
    ·
    7 days ago

    Rust

    use std::collections::HashMap;
    use itertools::Itertools;
    
    pub fn solve_part_1(input: &str) -> String {
        let mut mentors = 0;
        let mut pairs = 0;
        for ch in input.chars() {
            match ch {
                'A' => mentors += 1,
                'a' => pairs += mentors,
                _ => {}
            }
        }
        pairs.to_string()
    }
    
    pub fn solve_part_2(input: &str) -> String {
        let mut mentors: HashMap<char, i64> = HashMap::new();
        let mut pairs = 0;
        for ch in input.chars() {
            match ch {
                'A'..='Z' => *mentors.entry(ch).or_default() += 1,
                'a'..='z' => pairs += *mentors.entry(ch.to_ascii_uppercase()).or_default(),
                _ => panic!("unexpected character {ch}"),
            }
        }
        pairs.to_string()
    }
    
    pub fn solve_part_3(input: &str) -> String {
        let data: Vec<_> = input.chars().collect();
        let len = data.len();
        let mentors: HashMap<char, Vec<usize>> = data
            .iter()
            .enumerate()
            .map(|(i, ch)| (*ch, i))
            .into_group_map();
        let mut pairs: i64 = 0;
        for (squire_position, ch) in data.into_iter().enumerate() {
            if ch.is_ascii_lowercase() {
                for mentor_position in mentors.get(&ch.to_ascii_uppercase()).unwrap() {
                    if squire_position.abs_diff(*mentor_position) <= 1000 {
                        pairs += 1000;
                    } else if (squire_position as isize)
                        .wrapping_sub_unsigned(len)
                        .abs_diff(*mentor_position as isize)
                        <= 1000
                        || (*mentor_position as isize)
                            .wrapping_sub_unsigned(len)
                            .abs_diff(squire_position as isize)
                            <= 1000
                    {
                        pairs += 999;
                    }
                }
            }
        }
        pairs.to_string()
    }