diff options
Diffstat (limited to 'day1')
| -rw-r--r-- | day1/src/main.rs | 40 |
1 files changed, 22 insertions, 18 deletions
diff --git a/day1/src/main.rs b/day1/src/main.rs index aaee00b..5931c59 100644 --- a/day1/src/main.rs +++ b/day1/src/main.rs @@ -7,8 +7,8 @@ use std::{ #[derive(Debug, PartialEq, Eq)] enum Rotation { - Left(u16), - Right(u16), + Left(u32), + Right(u32), } #[derive(Debug)] @@ -20,7 +20,7 @@ impl FromStr for Rotation { fn from_str(s: &str) -> Result<Self, Self::Err> { let mut chars = s.chars(); let lr = chars.next().ok_or(ParseRotationError)?; - let n: u16 = chars + let n: u32 = chars .collect::<String>() .parse() .map_err(|_| ParseRotationError)?; @@ -42,29 +42,23 @@ impl Dial { } } -impl Add<u16> for Dial { +impl Add<u32> for Dial { type Output = Self; - fn add(self, rhs: u16) -> Self::Output { - let n = self.0 as u16; + fn add(self, rhs: u32) -> Self::Output { + let n = self.0 as u32; let m = (n + rhs) % 100; Self(m as u8) } } -impl Sub<u16> for Dial { +impl Sub<u32> for Dial { type Output = Self; - fn sub(self, rhs: u16) -> Self::Output { - let n = self.0 as u16; - - if rhs > n { - let m = 100 - (n + rhs) % 100; - Self(m as u8) - } else { - let m = n - rhs; - Self(m as u8) - } + fn sub(self, rhs: u32) -> Self::Output { + let n = self.0 as u32; + let m = (n + 100 - rhs % 100) % 100; + Self(m as u8) } } @@ -84,11 +78,13 @@ fn main() -> anyhow::Result<()> { .map(|s| s.parse().map_err(|_| anyhow!("bad rotation: {}", s))) .collect::<anyhow::Result<_>>()?; + println!("rotations: {}", rotations.len()); + let mut dial = Dial::new(); let mut zeros = 0; for rot in rotations { - println!("dial: {:?}", dial); + println!("dial: {}", dial.0); println!("rotation: {:?}", rot); match rot { @@ -133,10 +129,18 @@ mod tests { assert_eq!(dial + 50, 0); assert_eq!(dial + 100, 50); assert_eq!(dial + 150, 0); + assert_eq!(dial + 2 + 48, 0); + assert_eq!(dial + 45 + 60, 55); + assert_eq!(dial - 45, 5); + assert_eq!(dial - 45 - 10, 95); assert_eq!(dial - 1, 49); assert_eq!(dial - 68, 82); assert_eq!(dial - 68 - 30, 52); assert_eq!(dial - 100, 50); + assert_eq!(dial - 998, 52); + assert_eq!(dial - 150, 0); + assert_eq!(dial - 51, 99); + assert_eq!(dial - 50, 0); } } |
