Adding regex crate and day3 part 1 solution.

This commit is contained in:
Dan Chadwick
2024-12-08 22:57:11 -05:00
parent 979f4e348a
commit 73eb7211e6
6 changed files with 83 additions and 0 deletions

27
2024/rust/src/day3.rs Normal file
View File

@@ -0,0 +1,27 @@
use crate::Solution;
use std::fs;
use std::process;
use regex::Regex;
pub fn run_day_3() -> Solution {
let file = fs::read_to_string("./inputs/day3.txt").expect("Unable to parse file");
let re = Regex::new("mul\\((\\d{1,3}),(\\d{1,3})\\)").unwrap();
let mut part1_sum = 0;
for mul in re.captures_iter(&file) {
// Multiply the second and the third matches.
let num1: &u32 = &mul[1].parse().expect("Conversion failed");
let num2: &u32 = &mul[2].parse().expect("Conversion failed");
let total = num1 * num2;
part1_sum += total;
}
let solution = Solution {
day: 3,
answer_1: part1_sum.to_string(),
answer_2: "Not sure".to_string(),
};
solution
}

View File

@@ -4,6 +4,7 @@
use tabled::{Table, Tabled};
pub mod day1;
pub mod day2;
pub mod day3;
#[derive(Tabled)]
pub struct Solution {
@@ -31,8 +32,10 @@ fn main() {
let mut rows = Vec::new();
let day1 = day1::run_day_1();
let day2 = day2::run_day_2();
let day3 = day3::run_day_3();
rows.push(day1);
rows.push(day2);
rows.push(day3);
let table = Table::new(rows);