From 646145dd10b3a9549f82b237ee46726d728d3631 Mon Sep 17 00:00:00 2001 From: Josh Kingsley Date: Mon, 1 Dec 2025 14:57:26 +0200 Subject: Add day1 with Rotation struct and tests --- day1/src/main.rs | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 day1/src/main.rs (limited to 'day1/src') diff --git a/day1/src/main.rs b/day1/src/main.rs new file mode 100644 index 0000000..778fc6d --- /dev/null +++ b/day1/src/main.rs @@ -0,0 +1,52 @@ +use std::str::FromStr; + +#[derive(Debug, PartialEq, Eq)] +enum Rotation { + Left(u16), + Right(u16), +} + +struct ParseRotationError(String); + +impl FromStr for Rotation { + type Err = ParseRotationError; + + fn from_str(s: &str) -> Result { + let mut chars = s.chars(); + let lr = chars.next().ok_or_else(|| ParseRotationError(s.into()))?; + let n: u16 = chars + .collect::() + .parse() + .map_err(|_| ParseRotationError(s.into()))?; + + match lr { + 'L' => Ok(Self::Left(n)), + 'R' => Ok(Self::Right(n)), + _ => Err(ParseRotationError(s.into())), + } + } +} + +fn main() { + println!("Hello, world!"); +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn parse_rotation() { + assert!("L2" + .parse::() + .is_ok_and(|rot| rot == Rotation::Left(2))); + + assert!("R929" + .parse::() + .is_ok_and(|rot| rot == Rotation::Right(929))); + + assert!("M1".parse::().is_err()); + assert!("L".parse::().is_err()); + assert!("2".parse::().is_err()); + } +} -- cgit v1.2.3