DEV Community

Discussion on: AoC Day 1: Chronal Calibration

Collapse
 
shritesh profile image
Shritesh Bhattarai • Edited

I'm doing AoC with Rust too and the solution I came up with looks almost exactly like yours.

Part 1

pub fn callibrate(input: &[&str]) -> i32 {
    input.iter().map(|s| s.parse::<i32>().unwrap()).sum()
}

Part 2: I used a HashSet

pub fn twice(input: &[&str]) -> i32 {
    let mut numbers = input.iter().map(|s| s.parse::<i32>().unwrap()).cycle();

    let mut seen_results = HashSet::new();
    seen_results.insert(0);

    let mut sum = 0;

    loop {
        let num = numbers.next().unwrap();
        sum += num;

        if seen_results.contains(&sum) {
            return sum;
        } else {
            seen_results.insert(sum);
        }
    }
}

Instead of changing the main.rs file every day, I'll suggest creating a separate file for each day inside src/bin directory. That way, you can execute them using cargo run --bin day_1. Small but well thought out features like this is why I love Rust so much. For example, you can look at my repo at github.com/shritesh/advent-of-code

I hope to get through all of the challenges this year and learn from everyone here.

Collapse
 
rpalo profile image
Ryan Palo

Oooh! Good idea! I’m definitely doing that. Thanks!