aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Cargo.lock7
-rw-r--r--day1/Cargo.toml6
-rw-r--r--day1/src/main.rs52
3 files changed, 65 insertions, 0 deletions
diff --git a/Cargo.lock b/Cargo.lock
new file mode 100644
index 0000000..52d0c35
--- /dev/null
+++ b/Cargo.lock
@@ -0,0 +1,7 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 4
+
+[[package]]
+name = "day1"
+version = "0.1.0"
diff --git a/day1/Cargo.toml b/day1/Cargo.toml
new file mode 100644
index 0000000..8ffbbfb
--- /dev/null
+++ b/day1/Cargo.toml
@@ -0,0 +1,6 @@
+[package]
+name = "day1"
+version = "0.1.0"
+edition = "2024"
+
+[dependencies]
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<Self, Self::Err> {
+ let mut chars = s.chars();
+ let lr = chars.next().ok_or_else(|| ParseRotationError(s.into()))?;
+ let n: u16 = chars
+ .collect::<String>()
+ .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::<Rotation>()
+ .is_ok_and(|rot| rot == Rotation::Left(2)));
+
+ assert!("R929"
+ .parse::<Rotation>()
+ .is_ok_and(|rot| rot == Rotation::Right(929)));
+
+ assert!("M1".parse::<Rotation>().is_err());
+ assert!("L".parse::<Rotation>().is_err());
+ assert!("2".parse::<Rotation>().is_err());
+ }
+}