Skip to content
Snippets Groups Projects

AoC 2023 day 6

  • Clone with SSH
  • Clone with HTTPS
  • Embed
  • Share
    The snippet can be accessed without any authentication.
    Authored by s91149
    main.rs 1.37 KiB
    fn main() {
        let input = std::fs::read_to_string("../inputs/input.txt").unwrap();
        part_1(&input);
        part_2(&input);
    }
    
    fn part_1(input: &str) {
        let numbers: Vec<Vec<u32>> = input
            .lines()
            .map(|line| {
                line.split_whitespace()
                    .filter_map(|n| n.parse::<u32>().ok())
                    .collect::<Vec<u32>>()
            })
            .collect();
    
        let res: usize = numbers[0]
            .iter()
            .zip(numbers[1].iter())
            .map(|(time, record)| {
                (1..*time)
                    .map(|charging_time| charging_time * (time - charging_time))
                    .filter(|x| x > record)
                    .count()
            })
            .product();
    
        println!("part 1: {}", res);
    }
    
    fn part_2(input: &str) {
        let numbers: Vec<u64> = input
            .lines()
            .map(|line| {
                let numbers = line
                    .split_whitespace()
                    .filter(|n| n.parse::<u64>().is_ok())
                    .collect::<Vec<&str>>();
                let number_string: String = numbers.into_iter().collect();
                number_string.parse::<u64>().unwrap()
            })
            .collect();
    
        if let &[time, record] = &numbers[..] {
            let res = (1..time)
                .map(|charging_time| charging_time * (time - charging_time))
                .filter(|x| *x > record)
                .count();
    
            println!("part 2: {}", res);
        }
    }
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Finish editing this message first!
    Please register or to comment