diff --git a/day_07/src/equation.rs b/day_07/src/equation.rs index 0c6914c..510b709 100644 --- a/day_07/src/equation.rs +++ b/day_07/src/equation.rs @@ -66,11 +66,10 @@ impl Equation { accumulator } + const MULTIPLY_ADD_SET: [Op; 2] = [Op::Add, Op::Multiply]; pub fn solvable_multiply_add(&self) -> bool { - let op_combinations = create_permutations( - HashSet::from([Op::Add, Op::Multiply]), - self.components.len() - 1, - ); + let op_combinations = + create_permutations(&Equation::MULTIPLY_ADD_SET, self.components.len() - 1); for combination in op_combinations { if self.answer == self.compute_answer(&combination) { @@ -80,9 +79,10 @@ impl Equation { false } + const MULTIPLY_ADD_CONCAT_SET: [Op; 3] = [Op::Add, Op::Multiply, Op::Concat]; pub fn solvable_multiply_add_concat(&self) -> bool { let op_combinations = create_permutations( - HashSet::from([Op::Add, Op::Multiply, Op::Concat]), + &Equation::MULTIPLY_ADD_CONCAT_SET, self.components.len() - 1, ); diff --git a/day_07/src/equation/operator_set.rs b/day_07/src/equation/operator_set.rs index 307af72..c69c96d 100644 --- a/day_07/src/equation/operator_set.rs +++ b/day_07/src/equation/operator_set.rs @@ -14,11 +14,8 @@ struct PermutationCacheKey { } impl PermutationCacheKey { - fn new(size: usize, set: &HashSet) -> PermutationCacheKey { - PermutationCacheKey { - size, - set: set.iter().cloned().collect(), - } + fn new(size: usize, set: Vec) -> PermutationCacheKey { + PermutationCacheKey { size, set } } } @@ -42,7 +39,7 @@ fn store_operator_set_in_cache(cache_key: PermutationCacheKey, cache_value: Hash } fn create_permutations_recurse( - values: &HashSet, + values: &[Op], len: usize, permutations: &mut HashSet>, curr_permutation: Vec, @@ -59,13 +56,13 @@ fn create_permutations_recurse( } } -pub fn create_permutations(set: HashSet, len: usize) -> HashSet> { - let cache_key = PermutationCacheKey::new(len, &set); +pub fn create_permutations(set: &[Op], len: usize) -> HashSet> { + let cache_key = PermutationCacheKey::new(len, set.to_vec()); if let Some(cached_value) = check_operator_set_cache(&cache_key) { return cached_value; } let mut permutations = HashSet::new(); - create_permutations_recurse(&set, len, &mut permutations, vec![]); + create_permutations_recurse(set, len, &mut permutations, vec![]); store_operator_set_in_cache(cache_key, permutations.clone()); permutations }