▲ 1 r/adventofcode
[2025 Day 1 pt 2] [Rust] Suspected off by one but can't find it
I need help finding where my understanding is off because my answer agrees with the test case but doesn't give the right answer for the real input. I'm also using AOC to learn Rust so there's probably something I'm missing about the language itself as well.
The main idea is to add up the differences of the quotients of the before and after positions of the dial for each rotation. I've included my main.rs:
use std::env::args;
use std::fs::File;
use std::io::{BufRead, BufReader, Lines};
use std::path::Path;
const DIALSIZE: i16 = 100;
fn parse_input(path: &Path) -> impl Iterator<Item = i16> {
let file: File = File::open(path).unwrap(); // open the file
let lines: Lines<BufReader<File>> = BufReader::new(file).lines(); // iterator to the reader of the lines of the file
// iterator over the lines but with L replaced with - and R replaced with nothing to be positive
let rot_strs = lines.map(|line| -> String { line.unwrap().replace("L", "-").replace("R", "") });
rot_strs.map(|rot_str| -> i16 { rot_str.parse::<i16>().unwrap_or_default() })
}
fn print_dial(dial: i16) {
println!(
"Dial at {}",
(dial % DIALSIZE) + DIALSIZE * i16::from(dial.is_negative())
);
}
fn main() {
// open the file
// read line into buffer
// replace L with -1 or R with nothing
// parse into integer
// only work in raw position, never mod
// count += abs(div(old_pos + rotation, DIALSIZE) - div(old_pos, DIALSIZE))
// repeat
let args: Vec<String> = args().collect();
let path: &Path = Path::new(&args[1]);
let roterator = parse_input(path); // iterator over input lines that gives integers
let mut pre_rot: i16 = 0;
let mut post_rot: i16 = 50;
let mut pre_div: i16 = 0;
let mut post_div: i16 = 0;
let mut hits: i16 = 0;
roterator.for_each(|rot| {
// update dial position
pre_rot = post_rot;
post_rot += rot;
print_dial(post_rot);
// update zero hits
// div_euclid rounds toward negative infinity for negative lhs and postive rhs
// if postive or zero add zero, if negative add 1
pre_div = pre_rot.div_euclid(DIALSIZE) + 1 - i16::from(pre_rot.is_negative());
post_div = post_rot.div_euclid(DIALSIZE) + 1 - i16::from(post_rot.is_negative());
hits += (post_div - pre_div).abs();
});
println!("Final zero count: {}", hits);
}
u/NC01001110 — 3 days ago