refactor: make some quality improvements

This commit is contained in:
Lennard Brinkhaus 2023-12-01 11:32:32 +01:00
parent 51ad9ec790
commit 6b100ba0ea
3 changed files with 11 additions and 22 deletions

View File

@ -1,9 +1,5 @@
use crate::utils;
pub fn execute_task01(content: &str) { pub fn execute_task01(content: &str) {
let binding = utils::convert_to_string_slice(content); let iter = content.lines()
let iter =binding
.iter()
.map(|line| line.to_string()); .map(|line| line.to_string());
let calibration = sum_lines(iter); let calibration = sum_lines(iter);
@ -13,11 +9,9 @@ pub fn execute_task01(content: &str) {
pub fn execute_task02(content: &str) { pub fn execute_task02(content: &str) {
let binding = utils::convert_to_string_slice(content); let iter = content.lines()
let iter = binding
.iter()
.map(|line| { .map(|line| {
let mut full_str = "".to_owned(); let mut full_str = String::new();
for char in line.chars() { for char in line.chars() {
full_str.push(char); full_str.push(char);
@ -47,17 +41,10 @@ pub fn execute_task02(content: &str) {
} }
fn sum_lines(iter: impl Iterator<Item=String>) -> i32 { fn sum_lines(iter: impl Iterator<Item=String>) -> i32 {
iter.map(|line| iter
line .map(|line| format!("{}{}",
.chars() line.matches(|c: char| c.is_ascii_digit()).next().unwrap(),
.filter(|char| { line.rmatches(|c: char| c.is_ascii_digit()).next().unwrap()))
match char {
'0'..='9' => true,
_ => false
}
})
.collect::<String>())
.map(|number_str| format!("{}{}", number_str.chars().next().unwrap(), number_str.chars().last().unwrap()))
.map(|number_str| number_str.parse::<i32>().unwrap()) .map(|number_str| number_str.parse::<i32>().unwrap())
.sum() .sum()
} }

View File

@ -6,4 +6,5 @@ const CONTENT01: &'static str = include_str!("input/day01/input.txt");
fn main() { fn main() {
day01::execute_task01(CONTENT01); day01::execute_task01(CONTENT01);
day01::execute_task02(CONTENT01); day01::execute_task02(CONTENT01);
println!();
} }

View File

@ -1,5 +1,6 @@
#[allow(dead_code)]
pub fn convert_to_string_slice(content: &str) -> Vec<&str> { pub fn convert_to_string_slice(content: &str) -> Vec<&str> {
content.lines().collect() content.lines().collect()
} }