Compare commits
No commits in common. "3e76da9fa708f0e3ab20bbf1358aff194f89df8a" and "51ad9ec790785bf28c5690e21341d80888447a72" have entirely different histories.
3e76da9fa7
...
51ad9ec790
107
src/day01.rs
107
src/day01.rs
@ -1,72 +1,63 @@
|
|||||||
|
use crate::utils;
|
||||||
|
|
||||||
pub fn execute_task01(content: &str) {
|
pub fn execute_task01(content: &str) {
|
||||||
let calibration = solve_01(content);
|
let binding = utils::convert_to_string_slice(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 calibration = solve_02(content);
|
let binding = utils::convert_to_string_slice(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)
|
||||||
}
|
}
|
||||||
|
|
||||||
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|
|
||||||
.map(|line| format!("{}{}",
|
line
|
||||||
line.matches(|c: char| c.is_ascii_digit()).next().unwrap(),
|
.chars()
|
||||||
line.rmatches(|c: char| c.is_ascii_digit()).next().unwrap()))
|
.filter(|char| {
|
||||||
.map(|number_str| number_str.parse::<i32>().unwrap())
|
match char {
|
||||||
.sum()
|
'0'..='9' => true,
|
||||||
}
|
_ => false
|
||||||
|
}
|
||||||
#[test]
|
})
|
||||||
fn test_solve_01() {
|
.collect::<String>())
|
||||||
let test_input = r#"1abc2
|
.map(|number_str| format!("{}{}", number_str.chars().next().unwrap(), number_str.chars().last().unwrap()))
|
||||||
pqr3stu8vwx
|
.map(|number_str| number_str.parse::<i32>().unwrap())
|
||||||
a1b2c3d4e5f
|
.sum()
|
||||||
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);
|
|
||||||
}
|
}
|
@ -6,5 +6,4 @@ 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!();
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
|
|
||||||
|
|
||||||
#[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()
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user