this post was submitted on 01 Dec 2025
47 points (100.0% liked)

Advent Of Code

1154 readers
39 users here now

An unofficial home for the advent of code community on programming.dev! Other challenges are also welcome!

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.

Everybody Codes is another collection of programming puzzles with seasonal events.

EC 2025

AoC 2025

Solution Threads

M T W T F S S
1 2 3 4 5 6 7
8 9 10 11 12

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 2 years ago
MODERATORS
 

Day 1: Secret Entrance

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)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

FAQ

you are viewing a single comment's thread
view the rest of the comments
[โ€“] hades@programming.dev 5 points 2 days ago (3 children)

Rust

#[derive(Default)]
pub struct Day1Solver {
    input: Vec<i64>,
}

impl Solver for Day1Solver {
    fn presolve(&mut self, input: &str) {
        self.input = input
            .trim()
            .split("\n")
            .map(|line| {
                if let Some(n) = line.strip_prefix('L') {
                    -n.parse::<i64>().unwrap()
                } else if let Some(n) = line.strip_prefix('R') {
                    n.parse().unwrap()
                } else {
                    panic!("what: {line}");
                }
            })
            .collect();
    }

    fn solve_part_one(&mut self) -> String {
        let mut p = 50;
        let mut count = 0;
        for n in self.input.clone() {
            p += n;
            if p % 100 == 0 {
                count += 1;
            }
        }
        count.to_string()
    }

    fn solve_part_two(&mut self) -> String {
        let mut count = 0;
        let mut p = 1000000000050;
        for i in self.input.clone() {
            if p % 100 == 0 {
                count += (i / 100).abs();
            } else {
                count += ((p + i) / 100 - p / 100).abs();
                if i < 0 && (p + i) % 100 == 0 {
                    count += 1;
                }
            }
            p += i;
        }
        count.to_string()
    }
}
[โ€“] Gobbel2000@programming.dev 4 points 2 days ago (1 children)

Nice solution. Just a little Rust tip if you don't mind: In this case you can avoid cloning the input Vec in the loops by instead looping over references into the list with for n in self.input.iter() or simpler for n in &self.input. The only difference is that n will be of type &i64 instead of i64.

[โ€“] hades@programming.dev 3 points 2 days ago

Thanks! I donโ€™t mind tips at all.

[โ€“] Deebster@programming.dev 2 points 2 days ago (1 children)

I'm far from sure I understand what you're doing in part 2, but I think maybe you hit the same logic bug I did (I solved it with a shameful if statement that I will have to fix later).

[โ€“] hades@programming.dev 3 points 2 days ago (1 children)

the idea is that crossing the zero is easy to detect by dividing the total dial movement by 100. I.e. if you cross from 120 to 90 you will detect that 120/100=1 changed to 90/100=0. The only case when this doesnโ€™t work is when you stop at zero going in the negative direction, hence the extra if

[โ€“] Deebster@programming.dev 1 points 2 days ago (1 children)

The thing I couldn't comprehend was where 1000000000050 had come from.

[โ€“] hades@programming.dev 2 points 2 days ago

ah thatโ€™s just because i needed rounding towards infinity and not towards zero. In other words i wanted -10/100 to be -1 and not zero. But i couldnโ€™t figure it out on the spot, so i just made it never negative :)

[โ€“] VegOwOtenks@lemmy.world 1 points 2 days ago (1 children)

How could I run this code? Do you use some kind of framework for AoC?

[โ€“] hades@programming.dev 1 points 2 days ago

you could say that, yeah, it downloads the inputs, submits the answers, and keeps track of wrong answers. You can grab my entire repository here: https://github.com/hades/aoc25/tree/master