Make const "sets"
This commit is contained in:
@@ -66,11 +66,10 @@ impl Equation {
|
|||||||
accumulator
|
accumulator
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const MULTIPLY_ADD_SET: [Op; 2] = [Op::Add, Op::Multiply];
|
||||||
pub fn solvable_multiply_add(&self) -> bool {
|
pub fn solvable_multiply_add(&self) -> bool {
|
||||||
let op_combinations = create_permutations(
|
let op_combinations =
|
||||||
HashSet::from([Op::Add, Op::Multiply]),
|
create_permutations(&Equation::MULTIPLY_ADD_SET, self.components.len() - 1);
|
||||||
self.components.len() - 1,
|
|
||||||
);
|
|
||||||
|
|
||||||
for combination in op_combinations {
|
for combination in op_combinations {
|
||||||
if self.answer == self.compute_answer(&combination) {
|
if self.answer == self.compute_answer(&combination) {
|
||||||
@@ -80,9 +79,10 @@ impl Equation {
|
|||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const MULTIPLY_ADD_CONCAT_SET: [Op; 3] = [Op::Add, Op::Multiply, Op::Concat];
|
||||||
pub fn solvable_multiply_add_concat(&self) -> bool {
|
pub fn solvable_multiply_add_concat(&self) -> bool {
|
||||||
let op_combinations = create_permutations(
|
let op_combinations = create_permutations(
|
||||||
HashSet::from([Op::Add, Op::Multiply, Op::Concat]),
|
&Equation::MULTIPLY_ADD_CONCAT_SET,
|
||||||
self.components.len() - 1,
|
self.components.len() - 1,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -14,11 +14,8 @@ struct PermutationCacheKey {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl PermutationCacheKey {
|
impl PermutationCacheKey {
|
||||||
fn new(size: usize, set: &HashSet<Op>) -> PermutationCacheKey {
|
fn new(size: usize, set: Vec<Op>) -> PermutationCacheKey {
|
||||||
PermutationCacheKey {
|
PermutationCacheKey { size, set }
|
||||||
size,
|
|
||||||
set: set.iter().cloned().collect(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -42,7 +39,7 @@ fn store_operator_set_in_cache(cache_key: PermutationCacheKey, cache_value: Hash
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn create_permutations_recurse(
|
fn create_permutations_recurse(
|
||||||
values: &HashSet<Op>,
|
values: &[Op],
|
||||||
len: usize,
|
len: usize,
|
||||||
permutations: &mut HashSet<Vec<Op>>,
|
permutations: &mut HashSet<Vec<Op>>,
|
||||||
curr_permutation: 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>> {
|
pub fn create_permutations(set: &[Op], len: usize) -> HashSet<Vec<Op>> {
|
||||||
let cache_key = PermutationCacheKey::new(len, &set);
|
let cache_key = PermutationCacheKey::new(len, set.to_vec());
|
||||||
if let Some(cached_value) = check_operator_set_cache(&cache_key) {
|
if let Some(cached_value) = check_operator_set_cache(&cache_key) {
|
||||||
return cached_value;
|
return cached_value;
|
||||||
}
|
}
|
||||||
let mut permutations = HashSet::new();
|
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());
|
store_operator_set_in_cache(cache_key, permutations.clone());
|
||||||
permutations
|
permutations
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user