Make const "sets"

This commit is contained in:
2025-06-16 13:51:07 -05:00
parent ec0267a455
commit 16e726db6b
2 changed files with 11 additions and 14 deletions

View File

@@ -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,
);

View File

@@ -14,11 +14,8 @@ struct PermutationCacheKey {
}
impl PermutationCacheKey {
fn new(size: usize, set: &HashSet<Op>) -> PermutationCacheKey {
PermutationCacheKey {
size,
set: set.iter().cloned().collect(),
}
fn new(size: usize, set: Vec<Op>) -> 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<Op>,
values: &[Op],
len: usize,
permutations: &mut HashSet<Vec<Op>>,
curr_permutation: Vec<Op>,
@@ -59,13 +56,13 @@ fn create_permutations_recurse(
}
}
pub fn create_permutations(set: HashSet<Op>, len: usize) -> HashSet<Vec<Op>> {
let cache_key = PermutationCacheKey::new(len, &set);
pub fn create_permutations(set: &[Op], len: usize) -> HashSet<Vec<Op>> {
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
}