diff options
| author | Josh Kingsley <josh@joshkingsley.me> | 2025-12-03 22:16:57 +0200 |
|---|---|---|
| committer | Josh Kingsley <josh@joshkingsley.me> | 2025-12-03 22:16:57 +0200 |
| commit | 1496f52e32378d140a3b4680764d98caf512fbf1 (patch) | |
| tree | 6295dcc533c5f35e7b9681bf17ea7c1f5b93ad40 /day1/src | |
| parent | 603e6b37fcb141223e811ae96e9d2fa12cab5ead (diff) | |
Day 1 solution
Diffstat (limited to 'day1/src')
| -rw-r--r-- | day1/src/main.rs | 32 |
1 files changed, 23 insertions, 9 deletions
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; } } |
