Skip Navigation

🌟 - 2023 DAY 6 SOLUTIONS -🌟

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

39

You're viewing a single thread.

39 comments
  • A nice simple one today. And only a half second delay for part two instead of half an hour. What a treat. I could probably have nicer input parsing, but that seems to be the theme this year, so that will become a big focus of my next round through these I'm guessing. The algorithm here to get the winning possibilities could also probably be improved upon by figuring out what the number of seconds for the current record is, and only looping from there until hitting a number that doesn't win, as opposed to brute-forcing the whole loop.

    https://github.com/capitalpb/advent_of_code_2023/blob/main/src/solvers/day06.rs

    #[derive(Debug)]
    struct Race {
        time: u64,
        distance: u64,
    }
    
    impl Race {
        fn possible_ways_to_win(&self) -> usize {
            (0..=self.time)
                .filter(|time| time * (self.time - time) > self.distance)
                .count()
        }
    }
    
    pub struct Day06;
    
    impl Solver for Day06 {
        fn star_one(&self, input: &str) -> String {
            let mut race_data = input
                .lines()
                .map(|line| {
                    line.split_once(':')
                        .unwrap()
                        .1
                        .split_ascii_whitespace()
                        .filter_map(|number| number.parse::().ok())
                        .collect::>()
                })
                .collect::>();
    
            let times = race_data.pop().unwrap();
            let distances = race_data.pop().unwrap();
    
            let races = distances
                .into_iter()
                .zip(times)
                .map(|(time, distance)| Race { time, distance })
                .collect::>();
    
            races
                .iter()
                .map(|race| race.possible_ways_to_win())
                .fold(1, |acc, count| acc * count)
                .to_string()
        }
    
        fn star_two(&self, input: &str) -> String {
            let race_data = input
                .lines()
                .map(|line| {
                    line.split_once(':')
                        .unwrap()
                        .1
                        .replace(" ", "")
                        .parse::()
                        .unwrap()
                })
                .collect::>();
    
            let race = Race {
                time: race_data[0],
                distance: race_data[1],
            };
    
            race.possible_ways_to_win().to_string()
        }
    }
    
You've viewed 39 comments.