Compare commits

...

2 Commits

3 changed files with 61 additions and 50 deletions

View File

@ -1,63 +1,72 @@
use crate::utils;
pub fn execute_task01(content: &str) { pub fn execute_task01(content: &str) {
let binding = utils::convert_to_string_slice(content); let calibration = solve_01(content);
let iter =binding
.iter()
.map(|line| line.to_string());
let calibration = sum_lines(iter);
assert_eq!(calibration, 56465); assert_eq!(calibration, 56465);
println!("Day01 - Task01 - Calibration: {}", calibration) println!("Day01 - Task01 - Calibration: {}", calibration)
} }
pub fn execute_task02(content: &str) { pub fn execute_task02(content: &str) {
let binding = utils::convert_to_string_slice(content); let calibration = solve_02(content);
let iter = binding
.iter()
.map(|line| {
let mut full_str = "".to_owned();
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);
assert_eq!(calibration, 55902); assert_eq!(calibration, 55902);
println!("Day01 - Task02 - Calibration: {}", calibration) println!("Day01 - Task02 - Calibration: {}", calibration)
} }
fn sum_lines(iter: impl Iterator<Item=String>) -> i32 { pub fn solve_01(data: &str) -> i32 {
iter.map(|line| let iter = data.lines()
line .map(|line| line.to_string());
.chars() sum_lines(iter)
.filter(|char| {
match char {
'0'..='9' => true,
_ => false
} }
})
.collect::<String>()) pub fn solve_02(data: &str) -> i32 {
.map(|number_str| format!("{}{}", number_str.chars().next().unwrap(), number_str.chars().last().unwrap())) 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<Item=String>) -> i32 {
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::<i32>().unwrap()) .map(|number_str| number_str.parse::<i32>().unwrap())
.sum() .sum()
} }
#[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);
}

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()
} }