day_3: basic multiplication done

This commit is contained in:
2024-12-04 14:35:30 +01:00
parent d7a17e6de0
commit cc54e9121e
8 changed files with 107 additions and 1 deletions

View File

@@ -1,5 +1,6 @@
mod mon_02;
mod sun_01;
mod tue_03;
mod types;
mod utils;
@@ -19,4 +20,11 @@ pub fn historian_hysteria() {
// Monday 02
let safe_report_count = mon_02::check_reports_safety("./assets/day_2_reports_input");
println!("There are {safe_report_count} safe reports");
// Tuesday 03
let multiplication_added_result = tue_03::mull_it_over("./assets/day_3_instruction_input");
println!(
"The result of the sum of multiplications is {}",
multiplication_added_result
);
}

View File

@@ -0,0 +1,29 @@
use regex::Regex;
use utils::read_instruction_input;
use super::*;
pub fn mull_it_over(input: &str) -> MulNumber {
let instruction_input = read_instruction_input(input);
// NOTE: this regex could avoid process_mul_operation by capturing the values, but seems more readable this way
let multiply_regex = Regex::new(r"(mul\([0-9]+,[0-9]+\))").unwrap();
let mut sum_result: MulNumber = 0;
for mul in multiply_regex.captures_iter(&instruction_input) {
let multiplication_result = process_mul_operation(&mul[1]);
sum_result += multiplication_result;
}
sum_result
}
fn process_mul_operation(operation: &str) -> MulNumber {
let mul_regex = Regex::new(r"mul\(([0-9]+),([0-9]+)\)").unwrap();
for values in mul_regex.captures_iter(operation) {
return &values[1].parse::<MulNumber>().unwrap() * &values[2].parse::<MulNumber>().unwrap();
}
0
}

View File

@@ -1,7 +1,9 @@
// Historian Hysteria - Day 1
pub type Id = i32;
pub type Key = Id;
pub type Similarity = Id;
// Red-Nosed Reports - Day 2
pub type Level = i32;
pub type Report = Vec<Level>;
pub type ReportSafety = u32;
@@ -11,3 +13,6 @@ pub enum ReportDirection {
Increasing,
Decreasing,
}
// Mull It Over
pub type MulNumber = i32;

View File

@@ -32,6 +32,16 @@ pub fn read_report_list(input: &str) -> Vec<Report> {
report_list
}
pub fn read_instruction_input(input: &str) -> String {
let mut instructions = String::new();
for line in read_to_string(input).unwrap().lines() {
instructions.push_str(line);
}
instructions
}
pub fn calc_distance<T>(num_1: T, num_2: T) -> T
where
T: PartialOrd + Sub<Output = T>,