Advent of Code, Day 1, Part 2

C…
crusty.rustacean
Published on December 08, 2025 • Updated December 10, 2025

My thoughts, and solution, for Day 1, Part 2.

posts
programming rust advent-of-code
0 likes

I intended on writing everyday after I’d solved this year’s Advent of Code puzzles. I’m all the way deep into Day 9 now and I’ve written about so far was the first half of Day 1.

Here is my solution to Part 2 of Day 1.

This one was all about tricky edge cases. Here’s my code:

const DIAL_START_POSITION: i32 = 50;
const DIAL_RANGE: i32 = 100;

struct Dial {
    position: i32,
}

impl Dial {
    fn rotate(&mut self, direction: char, clicks: &i32) {
        self.position = match direction {
            'L' => (self.position - clicks).rem_euclid(DIAL_RANGE),
            'R' => (self.position + clicks).rem_euclid(DIAL_RANGE),
            _ => self.position,
        }
    }
}

fn main() {
    let mut dial = Dial {
        position: DIAL_START_POSITION,
    };

    // let puzzle_input = "L68\nL30\nR48\nL5\nR60\nL55\nL1\nL99\nR14\nL82";
    let puzzle_input = include_str!("../puzzle1_input.txt");

    let instructions: Vec<&str> = puzzle_input.split('\n').collect();
    let mut count = 0;
    let mut passed_count = 0;
    for instruction in instructions.iter() {
        let mut passed = 0;
        let direction = instruction.chars().next().unwrap();
        let clicks = &instruction[1..].parse::<i32>().unwrap();
        if direction == 'L' {
            if dial.position == 0 {
                if *clicks >= DIAL_RANGE {
                    passed_count += *clicks / DIAL_RANGE;
                    passed += 1;
                }
            } else if *clicks > dial.position {
                let laps = (*clicks - dial.position) / DIAL_RANGE;
                passed_count += 1 + laps;
                passed += 1;
            }
        }
        if direction == 'R' && (DIAL_RANGE - dial.position) < *clicks {
            passed += 1;
            let laps = (*clicks - (DIAL_RANGE - dial.position)) / DIAL_RANGE;
            passed_count += 1 + laps;
        }
        dial.rotate(direction, clicks);
        if dial.position == 0 && passed == 0 {
            count += 1;
        }
    }

    let total_count = count + passed_count;

    println!("Number of times dial was at zero: {}", count);
    println!("The number of times dial passed zero: {}", passed_count);
    println!("The total count is: {}", total_count);
}

In this one, not only do we have to count the number of times we land on 0, we have to count the number of times we pass 0 when going either left or right on the dial. Unfortunately, I couldn’t reason through it entirely on my own, Claude had to help a lot.

The trickiest edge case was this: What if the dial was already at zero before moving? That doesn’t really count as 0, does it?

Dealing with edge cases is a “hard computer science problem” most of the time and IMHO is where the true skill lies. You can get 80% or ever 90% of conditions addressed in your code relatively easily. It’s getting the last polish on that separates the true masters.

C…
crusty.rustacean

Comments

Loading comments...