diff --git a/src/day01.rs b/src/day01.rs index 061f737..8b73b7e 100644 --- a/src/day01.rs +++ b/src/day01.rs @@ -1,9 +1,5 @@ -use crate::utils; - pub fn execute_task01(content: &str) { - let binding = utils::convert_to_string_slice(content); - let iter =binding - .iter() + let iter = content.lines() .map(|line| line.to_string()); let calibration = sum_lines(iter); @@ -13,11 +9,9 @@ pub fn execute_task01(content: &str) { pub fn execute_task02(content: &str) { - let binding = utils::convert_to_string_slice(content); - let iter = binding - .iter() + let iter = content.lines() .map(|line| { - let mut full_str = "".to_owned(); + let mut full_str = String::new(); for char in line.chars() { full_str.push(char); @@ -47,17 +41,10 @@ pub fn execute_task02(content: &str) { } fn sum_lines(iter: impl Iterator) -> i32 { - iter.map(|line| - line - .chars() - .filter(|char| { - match char { - '0'..='9' => true, - _ => false - } - }) - .collect::()) - .map(|number_str| format!("{}{}", number_str.chars().next().unwrap(), number_str.chars().last().unwrap())) - .map(|number_str| number_str.parse::().unwrap()) - .sum() + iter + .map(|line| format!("{}{}", + line.matches(|c: char| c.is_ascii_digit()).next().unwrap(), + line.rmatches(|c: char| c.is_ascii_digit()).next().unwrap())) + .map(|number_str| number_str.parse::().unwrap()) + .sum() } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index a768668..c396808 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,4 +6,5 @@ const CONTENT01: &'static str = include_str!("input/day01/input.txt"); fn main() { day01::execute_task01(CONTENT01); day01::execute_task02(CONTENT01); + println!(); } diff --git a/src/utils.rs b/src/utils.rs index 70fa577..cfc69d9 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,5 +1,6 @@ +#[allow(dead_code)] pub fn convert_to_string_slice(content: &str) -> Vec<&str> { content.lines().collect() } \ No newline at end of file