From 72492db6eaa292d5f671e04bb41fc470036a1d08 Mon Sep 17 00:00:00 2001 From: Bearmine Date: Mon, 16 Jun 2025 12:24:18 -0500 Subject: [PATCH] Move over to using enum --- day_07/src/equation.rs | 21 ++++++--------------- day_07/src/equation/operator_set.rs | 28 +++++++++++++++++----------- 2 files changed, 23 insertions(+), 26 deletions(-) diff --git a/day_07/src/equation.rs b/day_07/src/equation.rs index b9987c1..f704598 100644 --- a/day_07/src/equation.rs +++ b/day_07/src/equation.rs @@ -1,12 +1,6 @@ mod operator_set; -use operator_set::create_operator_set; - -#[derive(Debug, Clone, Copy)] -enum Ops { - Multiply, - Add, -} +use operator_set::{Op, create_operator_set}; #[derive(Debug)] pub struct Equation { @@ -56,16 +50,13 @@ impl Equation { &self.components } - fn compute_answer(&self, operators: &[bool]) -> i64 { + fn compute_answer(&self, operators: &[Op]) -> i64 { let mut accumulator = self.components[0]; - for (i, op_bool) in operators.iter().enumerate() { - if *op_bool { - // Multiply - accumulator *= self.components[i + 1]; - } else { - // Add - accumulator += self.components[i + 1]; + for (i, op) in operators.iter().enumerate() { + match op { + Op::Multiply => accumulator *= self.components[i + 1], + Op::Add => accumulator += self.components[i + 1], } } diff --git a/day_07/src/equation/operator_set.rs b/day_07/src/equation/operator_set.rs index 2735e2b..3d79cd8 100644 --- a/day_07/src/equation/operator_set.rs +++ b/day_07/src/equation/operator_set.rs @@ -2,16 +2,22 @@ use std::collections::HashMap; use std::collections::HashSet; use std::sync::{LazyLock, Mutex}; -type OperatorCache = HashMap>>; +#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)] +pub enum Op { + Multiply, + Add, +} + +type OperatorCache = HashMap>>; static OPERATOR_SET_CACHE: LazyLock> = LazyLock::new(|| Mutex::new(HashMap::new())); -fn check_operator_set_cache(cache_key: usize) -> Option>> { +fn check_operator_set_cache(cache_key: usize) -> Option>> { OPERATOR_SET_CACHE.lock().unwrap().get(&cache_key).cloned() } -fn store_operator_set_in_cache(cache_key: usize, cache_value: HashSet>) { +fn store_operator_set_in_cache(cache_key: usize, cache_value: HashSet>) { OPERATOR_SET_CACHE .lock() .unwrap() @@ -19,32 +25,32 @@ fn store_operator_set_in_cache(cache_key: usize, cache_value: HashSet> } fn create_operator_set_recurse( - operators_permutation_set: &mut HashSet>, - operators: Vec, + operators_permutation_set: &mut HashSet>, + operators: Vec, ) { operators_permutation_set.insert(operators.clone()); - if !operators.contains(&false) { + if !operators.contains(&Op::Add) { return; } for i in 0..operators.len() { - if operators[i] { + if operators[i] == Op::Multiply { break; } let mut new_operators = operators.clone(); - new_operators[i] = true; + new_operators[i] = Op::Multiply; create_operator_set_recurse(operators_permutation_set, new_operators); } } -pub fn create_operator_set(num_of_operators: usize) -> HashSet> { +pub fn create_operator_set(num_of_operators: usize) -> HashSet> { // Check cache if let Some(cached_value) = check_operator_set_cache(num_of_operators) { return cached_value.clone(); } - let mut operator_sets: HashSet> = HashSet::new(); - create_operator_set_recurse(&mut operator_sets, vec![false; num_of_operators]); + let mut operator_sets: HashSet> = HashSet::new(); + create_operator_set_recurse(&mut operator_sets, vec![Op::Add; num_of_operators]); // Store value in cache store_operator_set_in_cache(num_of_operators, operator_sets.clone());