diff --git a/src/day01.rs b/src/day01.rs index 8b73b7e..72554e6 100644 --- a/src/day01.rs +++ b/src/day01.rs @@ -1,45 +1,40 @@ pub fn execute_task01(content: &str) { - let iter = content.lines() - .map(|line| line.to_string()); - let calibration = sum_lines(iter); + let calibration = solve_01(content); assert_eq!(calibration, 56465); println!("Day01 - Task01 - Calibration: {}", calibration) } - pub fn execute_task02(content: &str) { - let iter = content.lines() - .map(|line| { - let mut full_str = String::new(); - for char in line.chars() { - full_str.push(char); - - let copy = full_str.replace("one", "1") - .replace("two", "2") - .replace("three", "3") - .replace("four", "4") - .replace("five", "5") - .replace("six", "6") - .replace("seven", "7") - .replace("eight", "8") - .replace("nine", "9"); - - if copy != full_str { - full_str = copy; - full_str.push(char); - } - } - - full_str - }); - - let calibration = sum_lines(iter); + let calibration = solve_02(content); assert_eq!(calibration, 55902); println!("Day01 - Task02 - Calibration: {}", calibration) } +pub fn solve_01(data: &str) -> i32 { + let iter = data.lines() + .map(|line| line.to_string()); + sum_lines(iter) +} + +pub fn solve_02(data: &str) -> i32 { + let iter = data.lines() + .map(|line| { + line.replace("one", "o1e") + .replace("two", "t2o") + .replace("three", "t3e") + .replace("four", "f4r") + .replace("five", "f5e") + .replace("six", "s6x") + .replace("seven", "s7n") + .replace("eight", "e8t") + .replace("nine", "n9e") + }); + + sum_lines(iter) +} + fn sum_lines(iter: impl Iterator) -> i32 { iter .map(|line| format!("{}{}", @@ -47,4 +42,31 @@ fn sum_lines(iter: impl Iterator) -> i32 { line.rmatches(|c: char| c.is_ascii_digit()).next().unwrap())) .map(|number_str| number_str.parse::().unwrap()) .sum() -} \ No newline at end of file +} + +#[test] +fn test_solve_01() { + let test_input = r#"1abc2 +pqr3stu8vwx +a1b2c3d4e5f +treb7uchet"#; + + let solution = solve_01(test_input); + + assert_eq!(142, solution); +} + +#[test] +fn test_solve_02() { + let test_input = r#"two1nine +eightwothree +abcone2threexyz +xtwone3four +4nineeightseven2 +zoneight234 +7pqrstsixteen"#; + + let solution = solve_02(test_input); + + assert_eq!(281, solution); +}