refact: write better code and add some tests
This commit is contained in:
parent
6b100ba0ea
commit
3e76da9fa7
82
src/day01.rs
82
src/day01.rs
@ -1,45 +1,40 @@
|
|||||||
pub fn execute_task01(content: &str) {
|
pub fn execute_task01(content: &str) {
|
||||||
let iter = content.lines()
|
let calibration = solve_01(content);
|
||||||
.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 iter = content.lines()
|
let calibration = solve_02(content);
|
||||||
.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);
|
|
||||||
|
|
||||||
assert_eq!(calibration, 55902);
|
assert_eq!(calibration, 55902);
|
||||||
println!("Day01 - Task02 - Calibration: {}", calibration)
|
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<Item=String>) -> i32 {
|
fn sum_lines(iter: impl Iterator<Item=String>) -> i32 {
|
||||||
iter
|
iter
|
||||||
.map(|line| format!("{}{}",
|
.map(|line| format!("{}{}",
|
||||||
@ -48,3 +43,30 @@ fn sum_lines(iter: impl Iterator<Item=String>) -> i32 {
|
|||||||
.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);
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user