From 1dcdb86bedf9e6371945812d54369108d4fbb4ec Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Mon, 1 Dec 2025 19:16:34 +0200 Subject: [PATCH] Finish part-1 --- 2025/daily-template/src/part1.rs | 2 + 2025/daily-template/src/part2.rs | 2 + 2025/day-01/src/part1.rs | 95 ++++++++++++++++++++++++++++++-- 3 files changed, 94 insertions(+), 5 deletions(-) diff --git a/2025/daily-template/src/part1.rs b/2025/daily-template/src/part1.rs index 94144f7..0a00e1f 100644 --- a/2025/daily-template/src/part1.rs +++ b/2025/daily-template/src/part1.rs @@ -1,6 +1,8 @@ use miette::Result; #[tracing::instrument] +#[allow(clippy::missing_panics_doc)] +#[allow(clippy::missing_errors_doc)] pub fn process(input: &str) -> Result { todo!("day xx - part 1"); Ok(0) diff --git a/2025/daily-template/src/part2.rs b/2025/daily-template/src/part2.rs index cbcdee4..ede34a7 100644 --- a/2025/daily-template/src/part2.rs +++ b/2025/daily-template/src/part2.rs @@ -1,6 +1,8 @@ use miette::Result; #[tracing::instrument] +#[allow(clippy::missing_panics_doc)] +#[allow(clippy::missing_errors_doc)] pub fn process(input: &str) -> Result { todo!("day xx - part 2"); Ok(0) diff --git a/2025/day-01/src/part1.rs b/2025/day-01/src/part1.rs index 94144f7..b23a885 100644 --- a/2025/day-01/src/part1.rs +++ b/2025/day-01/src/part1.rs @@ -1,9 +1,85 @@ use miette::Result; +use std::str::FromStr; + +#[derive(Debug, Clone, Copy, PartialEq)] +enum Direction { + Left, + Right, +} + +impl FromStr for Direction { + type Err = String; + fn from_str(s: &str) -> std::result::Result { + match s.to_lowercase().trim() { + "l" => Ok(Self::Left), + "r" => Ok(Self::Right), + _ => Err("Wrong value".to_string()), + } + } +} + +#[derive(Debug, Clone, Copy)] +struct Rotation { + amount: i32, + direction: Direction, +} + +impl Rotation { + const fn right(self, dial: i32) -> i32 { + (dial + self.amount).rem_euclid(100) + } + + const fn left(self, dial: i32) -> i32 { + (dial - self.amount).rem_euclid(100) + } +} + +impl FromStr for Rotation { + type Err = String; + fn from_str(s: &str) -> std::result::Result { + let trimmed = s.trim(); + let (direction_str, amount_str) = trimmed.split_at(1); + let direction = direction_str.parse()?; + let amount = amount_str.parse::().map_err(|e| e.to_string())?; + + Ok(Self { amount, direction }) + } +} + +#[derive(Debug, Clone)] +struct Sequence { + rotations: Vec, +} + +impl FromStr for Sequence { + type Err = String; + fn from_str(s: &str) -> std::result::Result { + let rotations = s + .trim() + .lines() + .map(|line| line.parse::().unwrap()) + .collect::>(); + Ok(Self { rotations }) + } +} #[tracing::instrument] +#[allow(clippy::missing_panics_doc)] +#[allow(clippy::missing_errors_doc)] pub fn process(input: &str) -> Result { - todo!("day xx - part 1"); - Ok(0) + let sequence = input.parse::().unwrap(); + let mut count = 0; + sequence.rotations.iter().fold(50, |acc, rotation| { + let number = match rotation.direction { + Direction::Left => rotation.left(acc), + Direction::Right => rotation.right(acc), + }; + if number == 0 { + count += 1; + } + number + }); + Ok(count) } #[cfg(test)] @@ -12,9 +88,18 @@ mod tests { #[test] fn test_process() -> Result<()> { - let input = ""; - todo!("haven't built test yet"); - let result = 0; + let input = "L68 +L30 +R48 +L5 +R60 +L55 +L1 +L99 +R14 +L82 +"; + let result = 3; assert_eq!(process(input)?, result); Ok(()) }