Day 6: Wait for It


Megathread guidelines

  • 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)
  • Code block support is not fully rolled out yet but likely will be in the middle of the event. Try to share solutions as both code blocks and using something such as https://topaz.github.io/paste/ , pastebin, or github (code blocks to future proof it for when 0.19 comes out and since code blocks currently function in some apps and some instances as well if they are running a 0.19 beta)

FAQ

  • bugsmith@programming.dev
    link
    fedilink
    arrow-up
    2
    ·
    edit-2
    1 year ago

    Today’s problems felt really refreshing after yesterday.

    Solution in Rust 🦀

    View formatted code on GitLab

    Code
    use std::{
        collections::HashSet,
        env, fs,
        io::{self, BufRead, BufReader, Read},
    };
    
    fn main() -> io::Result<()> {
        let args: Vec = env::args().collect();
        let filename = &args[1];
        let file1 = fs::File::open(filename)?;
        let file2 = fs::File::open(filename)?;
        let reader1 = BufReader::new(file1);
        let reader2 = BufReader::new(file2);
    
        println!("Part one: {}", process_part_one(reader1));
        println!("Part two: {}", process_part_two(reader2));
        Ok(())
    }
    
    fn parse_data(reader: BufReader) -> Vec> {
        let lines = reader.lines().flatten();
        let data: Vec<_> = lines
            .map(|line| {
                line.split(':')
                    .last()
                    .expect("text after colon")
                    .split_whitespace()
                    .map(|s| s.parse::().expect("numbers"))
                    .collect::>()
            })
            .collect();
        data
    }
    
    fn calculate_ways_to_win(time: u64, dist: u64) -> HashSet {
        let mut wins = HashSet::::new();
        for t in 1..time {
            let d = t * (time - t);
            if d > dist {
                wins.insert(t);
            }
        }
        wins
    }
    
    fn process_part_one(reader: BufReader) -> u64 {
        let data = parse_data(reader);
        let results: Vec<_> = data[0].iter().zip(data[1].iter()).collect();
        let mut win_method_qty: Vec = Vec::new();
        for r in results {
            win_method_qty.push(calculate_ways_to_win(*r.0, *r.1).len() as u64);
        }
        win_method_qty.iter().product()
    }
    
    fn process_part_two(reader: BufReader) -> u64 {
        let data = parse_data(reader);
        let joined_data: Vec<_> = data
            .iter()
            .map(|v| {
                v.iter()
                    .map(|d| d.to_string())
                    .collect::>()
                    .join("")
                    .parse::()
                    .expect("all digits")
            })
            .collect();
    
        calculate_ways_to_win(joined_data[0], joined_data[1]).len() as u64
    }
    
    #[cfg(test)]
    mod tests {
        use super::*;
    
        const INPUT: &str = "Time:      7  15   30
    Distance:  9  40  200";
    
        #[test]
        fn test_process_part_one() {
            let input_bytes = INPUT.as_bytes();
            assert_eq!(288, process_part_one(BufReader::new(input_bytes)));
        }
    
        #[test]
        fn test_process_part_two() {
            let input_bytes = INPUT.as_bytes();
            assert_eq!(71503, process_part_two(BufReader::new(input_bytes)));
        }
    }