feat: implement day01
This commit is contained in:
parent
0be455036c
commit
51ad9ec790
63
src/day01.rs
Normal file
63
src/day01.rs
Normal file
@ -0,0 +1,63 @@
|
||||
use crate::utils;
|
||||
|
||||
pub fn execute_task01(content: &str) {
|
||||
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);
|
||||
println!("Day01 - Task01 - Calibration: {}", calibration)
|
||||
}
|
||||
|
||||
|
||||
pub fn execute_task02(content: &str) {
|
||||
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);
|
||||
println!("Day01 - Task02 - Calibration: {}", calibration)
|
||||
}
|
||||
|
||||
fn sum_lines(iter: impl Iterator<Item=String>) -> i32 {
|
||||
iter.map(|line|
|
||||
line
|
||||
.chars()
|
||||
.filter(|char| {
|
||||
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())
|
||||
.sum()
|
||||
}
|
1000
src/input/day01/input.txt
Normal file
1000
src/input/day01/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
@ -1,3 +1,9 @@
|
||||
mod day01;
|
||||
mod utils;
|
||||
|
||||
const CONTENT01: &'static str = include_str!("input/day01/input.txt");
|
||||
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
day01::execute_task01(CONTENT01);
|
||||
day01::execute_task02(CONTENT01);
|
||||
}
|
||||
|
5
src/utils.rs
Normal file
5
src/utils.rs
Normal file
@ -0,0 +1,5 @@
|
||||
|
||||
|
||||
pub fn convert_to_string_slice(content: &str) -> Vec<&str> {
|
||||
content.lines().collect()
|
||||
}
|
Loading…
Reference in New Issue
Block a user