star five

This commit is contained in:
2024-12-03 18:09:28 -05:00
parent 9942c26897
commit 5db8c90c9c
4 changed files with 118 additions and 0 deletions

47
Cargo.lock generated
View File

@@ -2,6 +2,53 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "aho-corasick"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916"
dependencies = [
"memchr",
]
[[package]]
name = "aoc-2024"
version = "0.1.0"
dependencies = [
"regex",
]
[[package]]
name = "memchr"
version = "2.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
[[package]]
name = "regex"
version = "1.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191"
dependencies = [
"aho-corasick",
"memchr",
"regex-automata",
"regex-syntax",
]
[[package]]
name = "regex-automata"
version = "0.4.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c"

View File

@@ -4,3 +4,4 @@ version = "0.1.0"
edition = "2021"
[dependencies]
regex = "1.11.1"

View File

@@ -4,6 +4,7 @@ mod star_one;
mod star_two;
mod star_three;
mod star_four;
mod star_five;
fn main() {
let args: Vec<String> = env::args().collect();
@@ -12,6 +13,7 @@ fn main() {
"2" => star_two::run(),
"3" => star_three::run(),
"4" => star_four::run(),
"5" => star_five::run(),
_ => unreachable!(),
}
}

68
src/star_five.rs Normal file
View File

@@ -0,0 +1,68 @@
use std::fs;
use regex::Regex;
pub fn run() {
let file = fs::read_to_string("./inputs/star_five.txt").unwrap();
let input = file.lines();
let str_instructions = filter_instructions(input);
let instructions = str_instructions.iter().map(|i| parse_instruction(i)).collect();
let result = run_instructions(instructions);
println!("Result: {}", result);
}
fn filter_instructions<'a, I>(str_lines: I) -> Vec<String>
where
I: IntoIterator<Item = &'a str>
{
let re = Regex::new(r"mul\([0-9]{1,3},[0-9]{1,3}\)").unwrap();
let mut instructions = vec![];
for str_line in str_lines {
instructions.extend(
re.find_iter(str_line).map(|s| String::from(s.as_str())).into_iter()
);
}
instructions
}
fn parse_instruction(instruction: &str) -> (isize, isize) {
let re = Regex::new(r"[0-9]{1,3},[0-9]{1,3}").unwrap();
let substring = re.find(instruction).unwrap().as_str();
let nums: Vec<isize> = substring.split(",").map(|s| s.to_string().parse::<isize>().unwrap()).collect();
(nums[0], nums[1])
}
fn run_instructions(instructions: Vec<(isize, isize)>) -> isize {
instructions.iter().map(|&e| e.0 * e.1).reduce(|a, b| a + b).unwrap_or(0)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_filter_instructions() {
assert_eq!(
filter_instructions(vec!["xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))"]),
vec!["mul(2,4)", "mul(5,5)", "mul(11,8)", "mul(8,5)"],
)
}
#[test]
fn test_parse_instruction() {
assert_eq!(parse_instruction("mul(2,4)"), (2, 4));
assert_eq!(parse_instruction("mul(5,5)"), (5, 5));
assert_eq!(parse_instruction("mul(11,8)"), (11, 8));
assert_eq!(parse_instruction("mul(8,5)"), (8, 5));
}
#[test]
fn test_run_instructions() {
assert_eq!(run_instructions(vec![(2, 4), (5, 5), (11, 8), (8, 5)]), 161);
assert_eq!(run_instructions(vec![]), 0);
}
}