re: AoC Day 1: Chronal Calibration VIEW POST

FULL DISCUSSION
 

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.

 

Oooh! Good idea! Iā€™m definitely doing that. Thanks!

code of conduct - report abuse