🎉 rust day1 first implementation

main
Pau Costa 2023-12-01 16:55:04 +01:00
commit 9df57c9733
5 changed files with 73 additions and 0 deletions

6
.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
**/.vscode
**/.idea
**/input.txt
**/debug/
**/target/
**/*rs.bk

7
rust/day1/Cargo.lock generated Normal file
View File

@ -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"

8
rust/day1/Cargo.toml Normal file
View File

@ -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]

1
rust/day1/README Normal file
View File

@ -0,0 +1 @@
Create a file in the root folder with the input called input.txt

51
rust/day1/src/main.rs Normal file
View File

@ -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::<i32>().unwrap();
}
return total_calibration_value
}
fn second_impl(contents: String) -> i32 {
return 0
}
fn number_string_to_int(probable_string: &String) -> Option<i32>{
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,
}
}