From 1496f52e32378d140a3b4680764d98caf512fbf1 Mon Sep 17 00:00:00 2001 From: Josh Kingsley Date: Wed, 3 Dec 2025 22:16:57 +0200 Subject: Day 1 solution --- day1/src/main.rs | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) (limited to 'day1/src/main.rs') diff --git a/day1/src/main.rs b/day1/src/main.rs index 50ab8e1..1e82218 100644 --- a/day1/src/main.rs +++ b/day1/src/main.rs @@ -44,17 +44,16 @@ impl Dial { match rot { Rotation::Left(n) => { - if pos != 0 && *n >= pos { - let quot = n.div_ceil(100); - RotateResult(quot, self - *n) - } else { - RotateResult(0, self - *n) - } + let quot: u32 = n / 100; + RotateResult( + quot + if pos > 0 && n % 100 >= pos { 1 } else { 0 }, + self - *n, + ) } Rotation::Right(n) => { if n + pos >= 100 { - let quot = n.div_ceil(100); + let quot = n / 100 + (if pos + n % 100 >= 100 { 1 } else { 0 }); RotateResult(quot, self + *n) } else { RotateResult(0, self + *n) @@ -119,6 +118,8 @@ fn main() -> anyhow::Result<()> { total_zeros += zeros; dial = new_dial; + + eprintln!("rotation: {:?}, dial: {}, zeros: {}", rot, dial.0, zeros); } println!("Password: {}", exact_zeros); @@ -197,12 +198,25 @@ mod tests { (Rotation::Right(1000), 10, 50), (Rotation::Right(48), 0, 98), (Rotation::Left(651), 6, 47), + (Rotation::Right(651), 6, 98), + (Rotation::Right(203), 3, 1), + (Rotation::Left(1), 1, 0), + (Rotation::Left(100), 1, 0), + (Rotation::Right(100), 1, 0), ]; for (rot, expected_zeros, expected_dial) in &rotations { let RotateResult(zeros, new_dial) = dial.rotate(rot); - assert_eq!(&zeros, expected_zeros, "rotating with {:?}", rot); - assert_eq!(&new_dial, expected_dial, "rotating with {:?}", rot); + assert_eq!( + &zeros, expected_zeros, + "rotating from {} with {:?}", + dial.0, rot + ); + assert_eq!( + &new_dial, expected_dial, + "rotating from {} with {:?}", + dial.0, rot + ); dial = new_dial; } } -- cgit v1.2.3