feat: cheeky day 3 part 1 solution

This commit is contained in:
Zhongheng Liu 2024-12-12 20:02:28 +02:00
commit c0acf9524a
Signed by: steven
GPG key ID: 805A28B071DAD84B
3 changed files with 43 additions and 0 deletions

38
src/day3.rs Normal file
View file

@ -0,0 +1,38 @@
use std::fs;
fn check_mul(instruction: &str) -> bool {
if instruction.find(',').is_none() { return false; }
let (first_part, second_part) = instruction.split_once(',').expect("Expected mul to split");
let first_num = &first_part[4..];
let second_num = &second_part[..second_part.len() - 1];
for digit in first_num.chars() {
if !digit.is_numeric() {
return false;
}
}
for digit in second_num.chars() {
if !digit.is_numeric() {
return false;
}
}
true
}
fn eval_mul(instruction: &str) -> i32 {
let (first_part, second_part) = instruction.split_once(',').expect("Expected mul to split");
let first_num = str::parse::<i32>(&first_part[4..]).expect("Expect parse success");
let second_num =
str::parse::<i32>(&second_part[..second_part.len() - 1]).expect("Expect parse success");
first_num * second_num
}
pub fn part1(input_file_path: &str) {
let raw_str = fs::read_to_string(input_file_path).expect("File read error!!");
let mut total: i32 = 0;
raw_str.rmatch_indices("mul(").for_each(|(index, string)| {
println!("{} --> {}", index, string);
let tmp = raw_str.split_at(index).1;
let end_index = tmp.find(')').expect("expected a closure");
let trim_str = tmp.split_at(end_index + 1).0;
println!("{}", trim_str);
total += if check_mul(trim_str) {eval_mul(trim_str)} else {0};
});
println!("Eval result: {}", total);
}

View file

@ -4,9 +4,11 @@ use std::{
};
mod day1;
mod day2;
mod day3;
fn main() {
day1::part1("./day1.input");
day1::part2("./day1.input");
day2::part1("./day2.input");
day2::part2("./day2.input");
day3::part1("./day3.input");
}

3
src/test.rs Normal file
View file

@ -0,0 +1,3 @@
fn main(
)