Quest 3: The Deepest Fit

  • 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/

Also, don’t wait for me to make these posts, feel free to post yourself :)

  • hades@programming.devOPM
    link
    fedilink
    arrow-up
    2
    ·
    17 hours ago

    Rust

    pub fn solve_part_1(input: &str) -> String {
        let mut crates: Vec<i64> = input.split(",").map(|s| s.parse().unwrap()).collect();
        crates.sort();
        let mut monotonic_subsequence = vec![crates[0]];
        for size in crates.into_iter().skip(1) {
            if size == *monotonic_subsequence.last().unwrap() {
                continue;
            }
            monotonic_subsequence.push(size);
        }
        monotonic_subsequence.iter().sum::<i64>().to_string()
    }
    
    pub fn solve_part_2(input: &str) -> String {
        let mut crates: Vec<i64> = input.split(",").map(|s| s.parse().unwrap()).collect();
        crates.sort();
        let mut monotonic_subsequence = vec![crates[0]];
        for size in crates.into_iter().skip(1) {
            if size == *monotonic_subsequence.last().unwrap() {
                continue;
            }
            monotonic_subsequence.push(size);
            if monotonic_subsequence.len() >= 20 {
                break;
            }
        }
        monotonic_subsequence.iter().sum::<i64>().to_string()
    }
    
    pub fn solve_part_3(input: &str) -> String {
        let mut crates: Vec<i64> = input.split(",").map(|s| s.parse().unwrap()).collect();
        crates.sort();
        let mut monotonic_subsequences = vec![vec![crates[0]]];
        for size in crates.into_iter().skip(1) {
            let updateable_sequence = monotonic_subsequences
                .iter_mut()
                .find(|v| *v.last().unwrap() < size);
            match updateable_sequence {
                Some(v) => {
                    v.push(size);
                }
                None => {
                    monotonic_subsequences.push(vec![size]);
                }
            }
        }
        monotonic_subsequences.len().to_string()
    }