diff --git a/2025/Cargo.lock b/2025/Cargo.lock index e5cb7a3..cd1117f 100644 --- a/2025/Cargo.lock +++ b/2025/Cargo.lock @@ -130,6 +130,21 @@ dependencies = [ "tracing-subscriber", ] +[[package]] +name = "day-03" +version = "0.1.0" +dependencies = [ + "divan", + "itertools", + "miette", + "nom", + "rstest", + "test-log", + "thiserror", + "tracing", + "tracing-subscriber", +] + [[package]] name = "divan" version = "0.1.21" diff --git a/2025/daily-template/src/part1.rs b/2025/daily-template/src/part1.rs index 0a00e1f..1639d01 100644 --- a/2025/daily-template/src/part1.rs +++ b/2025/daily-template/src/part1.rs @@ -1,9 +1,7 @@ -use miette::Result; - #[tracing::instrument] #[allow(clippy::missing_panics_doc)] #[allow(clippy::missing_errors_doc)] -pub fn process(input: &str) -> Result { +pub fn process(input: &str) -> miette::Result { todo!("day xx - part 1"); Ok(0) } @@ -13,7 +11,7 @@ mod tests { use super::*; #[test] - fn test_process() -> Result<()> { + fn test_process() -> miette::Result<()> { let input = ""; todo!("haven't built test yet"); let result = 0; diff --git a/2025/day-03/src/part1.rs b/2025/day-03/src/part1.rs index 0a00e1f..184e40c 100644 --- a/2025/day-03/src/part1.rs +++ b/2025/day-03/src/part1.rs @@ -1,11 +1,75 @@ -use miette::Result; +use itertools::Itertools; +use std::{ops::Add, str::FromStr}; + +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] +struct Joltage(u8); + +impl Add for Joltage { + type Output = Self; + fn add(self, rhs: Self) -> Self::Output { + Self(self.0 * 10 + rhs.0) + } +} + +impl Add for &Joltage { + type Output = Joltage; + fn add(self, rhs: Self) -> Self::Output { + Joltage(self.0 * 10 + rhs.0) + } +} + +impl From for usize { + fn from(value: Joltage) -> Self { + Self::from(value.0) + } +} + +impl TryFrom for Joltage { + type Error = String; + fn try_from(ch: char) -> std::result::Result { + let s = ch.to_string(); + let value = s.parse::().map_err(|e| e.to_string())?; + Ok(Self(value)) + } +} +#[derive(Debug, Clone)] +struct Bank(Vec); + +impl Bank { + fn get_max_value(&self) -> usize { + let mut clone = self.0.clone(); + clone.truncate(self.0.len() - 1); + let max1 = clone.iter().max().unwrap(); + let (pos1, _) = self.0.iter().find_position(|&x| x == max1).unwrap(); + let list = self.0.clone().split_off(pos1 + 1); + let max2 = list.iter().max().unwrap(); + (max1 + max2).into() + } +} + +impl FromStr for Bank { + type Err = String; + fn from_str(s: &str) -> std::result::Result { + let bank = s + .trim() + .chars() + .map(Joltage::try_from) + .collect::, String>>()?; + + Ok(Self(bank)) + } +} #[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) +pub fn process(input: &str) -> miette::Result { + let banks = input + .lines() + .map(|line| Bank::from_str(line).unwrap()) + .map(|bank| bank.get_max_value()) + .sum(); + Ok(banks) } #[cfg(test)] @@ -13,10 +77,13 @@ mod tests { use super::*; #[test] - fn test_process() -> Result<()> { - let input = ""; - todo!("haven't built test yet"); - let result = 0; + fn test_process() -> miette::Result<()> { + let input = "987654321111111 +811111111111119 +234234234234278 +818181911112111 +"; + let result = 357; assert_eq!(process(input)?, result); Ok(()) }