Day 01 - Chronal Calibration

parsingcyclevisited

Language: Rust

Problem https://adventofcode.com/2018/day/1


Part 1: A list of signed integers like +3 or -7. Apply them all in order starting from 0 and return the final frequency. Rust’s parse::<i32>() handles the sign just fine, so it’s a straightforward sum:

input.lines()
    .filter_map(|line| line.parse::<i32>().ok())
    .sum::<i32>()
    .to_string()

Part 2: Cycle through the list repeatedly until a frequency is seen for the second time. The list may need to loop many times before a repeat shows up.

I collect the numbers first, then use .cycle() to loop them indefinitely. A HashSet tracks every frequency seen, and insert returns false the moment there’s a duplicate:

for num in numbers.iter().cycle() {
    freq += num;
    if !visited.insert(freq) {
        return freq.to_string();
    }
}

.cycle() on an iterator handles the looping cleanly without any index management.


Solution: https://github.com/Elyrial/AdventOfCode/blob/main/src/solutions/year2018/day01.rs

No C writeup yet.