this post was submitted on 06 Dec 2023
23 points (96.0% liked)

Advent Of Code

805 readers
10 users here now

An unofficial home for the advent of code community on programming.dev!

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

AoC 2024

Solution Threads

M T W T F S S
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 18 20 21 22
23 24 25

Rules/Guidelines

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

founded 1 year ago
MODERATORS
 

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

you are viewing a single comment's thread
view the rest of the comments
[โ€“] capitalpb@programming.dev 3 points 11 months ago

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()
    }
}