Compare commits

...

2 Commits

Author SHA1 Message Date
Pau Costa cd250ddd35 second part of day 1 2023-12-07 16:57:46 +01:00
Pau Costa 8ef1350090 modified gitignore with glob matching 2023-12-07 15:15:58 +01:00
2 changed files with 22 additions and 29 deletions

12
.gitignore vendored
View File

@ -1,17 +1,11 @@
**/.vscode **/.vscode
**/.idea **/.idea
**/input.txt
**/debug/ **/debug/
**/target/ **/target/
**/*rs.bk **/*rs.bk
**/calibration_document.txt
**/.vsidx **/.vsidx
**/.suo **/.suo
/CSharp/AdventOfCode/AdventOfCode/obj **/obj
/CSharp/AdventOfCode/AdventOfCode/bin **/bin
/CSharp/AdventOfCode/.vs **/.vs
/CSharp/AdventOfCode/DayOne/obj
/CSharp/AdventOfCode/DayOne/bin
/CSharp/AdventOfCode/DayTwo/obj
/CSharp/AdventOfCode/DayTwo/bin
*.txt *.txt

View File

@ -3,11 +3,13 @@ use std::fs;
fn main() { fn main() {
let contents = fs::read_to_string("input.txt").expect("Could not read the file"); let contents = fs::read_to_string("input.txt").expect("Could not read the file");
let first_total_calibration_value = first_impl(contents); let first_total_calibration_value = first_impl(&contents);
println!("The total value is: {first_total_calibration_value}"); println!("The total value is: {first_total_calibration_value}");
let second_total_calibration_value = second_impl(&contents);
println!("The second total value is: {second_total_calibration_value}")
} }
fn first_impl(contents: String) -> i32 { fn first_impl(contents: &String) -> i32 {
let mut total_calibration_value = 0; let mut total_calibration_value = 0;
for line in contents.split_whitespace() { for line in contents.split_whitespace() {
@ -30,22 +32,19 @@ fn first_impl(contents: String) -> i32 {
} }
return total_calibration_value return total_calibration_value
} }
fn second_impl(contents: &String) -> i32 {
fn second_impl(contents: String) -> i32 { first_impl(&number_string_to_int(contents))
return 0
} }
fn number_string_to_int(probable_string: &String) -> Option<i32>{ fn number_string_to_int(probable_string: &String) -> String{
match probable_string.as_str() { probable_string.clone()
"nine" => Some(9), .replace("one", "on1ne")
"eight" => Some(8), .replace("two", "tw2wo")
"seven" => Some(7), .replace("three", "thre3hree")
"six" => Some(6), .replace("four","fou4our")
"five" => Some(5), .replace("five", "fiv5ive")
"four" => Some(4), .replace("six", "si6ix")
"three" => Some(3), .replace("seven", "seve7even")
"two" => Some(2), .replace("eight", "eigh8ight")
"one" => Some(1), .replace("nine", "nin9ine")
_ => None,
}
} }