From df6721b198060c5013b3d0d141241a5391f29881 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20LUDWIG?= Date: Sat, 2 Dec 2023 23:50:07 +0100 Subject: [PATCH] refactor: day 1 to separate lib and main --- .editorconfig | 4 --- day_1/src/lib.rs | 79 ++++++++++++++++++++++++++++++++++++++++++++++ day_1/src/main.rs | 80 +---------------------------------------------- 3 files changed, 80 insertions(+), 83 deletions(-) create mode 100644 day_1/src/lib.rs diff --git a/.editorconfig b/.editorconfig index 8b423bf..8d59d07 100644 --- a/.editorconfig +++ b/.editorconfig @@ -9,7 +9,3 @@ end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true - -[*.txt] -trim_trailing_whitespace = unset -insert_final_newline = unset diff --git a/day_1/src/lib.rs b/day_1/src/lib.rs new file mode 100644 index 0000000..89c029e --- /dev/null +++ b/day_1/src/lib.rs @@ -0,0 +1,79 @@ +pub fn part_1(input: &str) -> usize { + input + .lines() + .map(|line| { + let characters_digits = line + .chars() + .filter(|&character| character.is_ascii_digit()) + .collect::>(); + + let first_digit = characters_digits.first().unwrap_or(&'0').to_owned(); + let last_digit = characters_digits.last().unwrap_or(&'0').to_owned(); + let number = format!("{}{}", first_digit, last_digit); + let number: usize = number.parse().expect("Should parse as a `usize`."); + number + }) + .sum() +} + +pub fn part_2(input: &str) -> usize { + let numbers_spelled_out_with_letters = [ + "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", + ]; + + input + .lines() + .map(|line| { + let mut characters_digits: Vec = vec![]; + let mut temporary = String::from(""); + for character in line.chars() { + temporary += &character.to_string(); + + let mut temporary_spelled_number_index = None; + for (index, spelled_number) in numbers_spelled_out_with_letters.iter().enumerate() { + if temporary.contains(spelled_number) { + temporary_spelled_number_index = Some(index); + break; + } + } + if let Some(temporary_spelled_number_index) = temporary_spelled_number_index { + let number = temporary_spelled_number_index + 1; + characters_digits.push( + number + .to_string() + .chars() + .next() + .expect("Number should be single-character digit."), + ); + temporary = character.to_string(); + } + + if character.is_ascii_digit() { + characters_digits.push(character); + temporary = String::from(""); + } + } + + let first_digit = characters_digits.first().unwrap_or(&'0').to_owned(); + let last_digit = characters_digits.last().unwrap_or(&'0').to_owned(); + let number = format!("{}{}", first_digit, last_digit); + let number: usize = number.parse().expect("Should parse as a `usize`."); + number + }) + .sum() +} + +#[cfg(test)] +mod day_1_tests { + use super::*; + + #[test] + fn test_part_1_example() { + assert_eq!(part_1(include_str!("../input_example_1.txt")), 142); + } + + #[test] + fn test_part_2_example() { + assert_eq!(part_2(include_str!("../input_example_2.txt")), 281); + } +} diff --git a/day_1/src/main.rs b/day_1/src/main.rs index 06ec0d4..e6160aa 100644 --- a/day_1/src/main.rs +++ b/day_1/src/main.rs @@ -1,67 +1,4 @@ -fn part_1(input: &str) -> i32 { - input - .lines() - .map(|line| { - let characters_digits = line - .chars() - .filter(|&character| character.is_ascii_digit()) - .collect::>(); - - let first_digit = characters_digits.first().unwrap_or(&'0').to_owned(); - let last_digit = characters_digits.last().unwrap_or(&'0').to_owned(); - let number = format!("{}{}", first_digit, last_digit); - let number: i32 = number.parse().expect("Should parse as a i32."); - number - }) - .sum() -} - -fn part_2(input: &str) -> i32 { - let numbers_spelled_out_with_letters = [ - "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", - ]; - - input - .lines() - .map(|line| { - let mut characters_digits: Vec = vec![]; - let mut temporary = String::from(""); - for character in line.chars() { - temporary += &character.to_string(); - - let mut temporary_spelled_number_index = None; - for (index, spelled_number) in numbers_spelled_out_with_letters.iter().enumerate() { - if temporary.contains(spelled_number) { - temporary_spelled_number_index = Some(index); - break; - } - } - if let Some(temporary_spelled_number_index) = temporary_spelled_number_index { - let number = temporary_spelled_number_index + 1; - characters_digits.push( - number - .to_string() - .chars() - .next() - .expect("Number should be single-character digit."), - ); - temporary = character.to_string(); - } - - if character.is_ascii_digit() { - characters_digits.push(character); - temporary = String::from(""); - } - } - - let first_digit = characters_digits.first().unwrap_or(&'0').to_owned(); - let last_digit = characters_digits.last().unwrap_or(&'0').to_owned(); - let number = format!("{}{}", first_digit, last_digit); - let number: i32 = number.parse().expect("Should parse as a number."); - number - }) - .sum() -} +use day_1::{part_1, part_2}; fn main() { let input = include_str!("../input.txt"); @@ -69,18 +6,3 @@ fn main() { println!("Answer Part 1: {}", part_1(input)); println!("Answer Part 2: {}", part_2(input)); } - -#[cfg(test)] -mod day_1_tests { - use super::*; - - #[test] - fn test_part_1_example() { - assert_eq!(142, part_1(include_str!("../input_example_1.txt"))); - } - - #[test] - fn test_part_2_example() { - assert_eq!(281, part_2(include_str!("../input_example_2.txt"))); - } -}