commit 9df57c97333feb3088a1548786a32059170f3ce3 Author: Pau Costa Date: Fri Dec 1 16:55:04 2023 +0100 :tada: rust day1 first implementation diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..dae6469 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +**/.vscode +**/.idea +**/input.txt +**/debug/ +**/target/ +**/*rs.bk diff --git a/rust/day1/Cargo.lock b/rust/day1/Cargo.lock new file mode 100644 index 0000000..ae5bfe8 --- /dev/null +++ b/rust/day1/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "day1" +version = "0.1.0" diff --git a/rust/day1/Cargo.toml b/rust/day1/Cargo.toml new file mode 100644 index 0000000..a3c4e52 --- /dev/null +++ b/rust/day1/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "day1" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/rust/day1/README b/rust/day1/README new file mode 100644 index 0000000..19ba663 --- /dev/null +++ b/rust/day1/README @@ -0,0 +1 @@ +Create a file in the root folder with the input called input.txt \ No newline at end of file diff --git a/rust/day1/src/main.rs b/rust/day1/src/main.rs new file mode 100644 index 0000000..a8d37ae --- /dev/null +++ b/rust/day1/src/main.rs @@ -0,0 +1,51 @@ +use std::fs; + +fn main() { + let contents = fs::read_to_string("input.txt").expect("Could not read the file"); + + let first_total_calibration_value = first_impl(contents); + println!("The total value is: {first_total_calibration_value}"); +} + +fn first_impl(contents: String) -> i32 { + let mut total_calibration_value = 0; + + for line in contents.split_whitespace() { + let mut calibration_value = String::new(); + + for character in line.chars() { + let _my_int: u32 = match character.to_digit(10) { + Some(digit) => digit, + _ => continue, + }; + if calibration_value.chars().count() > 1 { + _ = calibration_value.pop(); + } + calibration_value.push(character); + } + if calibration_value.chars().count() == 1 { + calibration_value.push_str(&calibration_value.clone()); + } + total_calibration_value += calibration_value.parse::().unwrap(); + } + return total_calibration_value +} + +fn second_impl(contents: String) -> i32 { + return 0 +} + +fn number_string_to_int(probable_string: &String) -> Option{ + match probable_string.as_str() { + "nine" => Some(9), + "eight" => Some(8), + "seven" => Some(7), + "six" => Some(6), + "five" => Some(5), + "four" => Some(4), + "three" => Some(3), + "two" => Some(2), + "one" => Some(1), + _ => None, + } +} \ No newline at end of file