Add caching back in

This commit is contained in:
2025-06-16 13:32:26 -05:00
parent 3815d496b9
commit f235c46bd4
2 changed files with 36 additions and 17 deletions

View File

@@ -55,7 +55,7 @@ impl Equation {
match op { match op {
Op::Multiply => accumulator *= self.components[i + 1], Op::Multiply => accumulator *= self.components[i + 1],
Op::Add => accumulator += self.components[i + 1], Op::Add => accumulator += self.components[i + 1],
Op::CONCAT => { Op::Concat => {
let accumulator_string = format!("{}", accumulator); let accumulator_string = format!("{}", accumulator);
let component_string = format!("{}", self.components[i + 1]); let component_string = format!("{}", self.components[i + 1]);
accumulator = (accumulator_string + &component_string).parse().unwrap(); accumulator = (accumulator_string + &component_string).parse().unwrap();
@@ -82,7 +82,7 @@ impl Equation {
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]), HashSet::from([Op::Add, Op::Multiply, Op::Concat]),
self.components.len() - 1, self.components.len() - 1,
); );

View File

@@ -4,27 +4,42 @@ use std::collections::HashSet;
pub enum Op { pub enum Op {
Multiply, Multiply,
Add, Add,
CONCAT, Concat,
} }
// use std::collections::HashMap; #[derive(Debug, Clone, Hash, PartialEq, Eq)]
// use std::sync::{LazyLock, Mutex}; struct PermutationCacheKey {
size: usize,
set: Vec<Op>,
}
// type OperatorCache = HashMap<usize, HashSet<Vec<Op>>>; impl PermutationCacheKey {
fn new(size: usize, set: &HashSet<Op>) -> PermutationCacheKey {
PermutationCacheKey {
size,
set: set.iter().cloned().collect(),
}
}
}
// static OPERATOR_SET_CACHE: LazyLock<Mutex<OperatorCache>> = use std::collections::HashMap;
// LazyLock::new(|| Mutex::new(HashMap::new())); use std::sync::{LazyLock, Mutex};
// fn check_operator_set_cache(cache_key: usize) -> Option<HashSet<Vec<Op>>> { type OperatorCache = HashMap<PermutationCacheKey, HashSet<Vec<Op>>>;
// OPERATOR_SET_CACHE.lock().unwrap().get(&cache_key).cloned()
// }
// fn store_operator_set_in_cache(cache_key: usize, cache_value: HashSet<Vec<Op>>) { static OPERATOR_SET_CACHE: LazyLock<Mutex<OperatorCache>> =
// OPERATOR_SET_CACHE LazyLock::new(|| Mutex::new(HashMap::new()));
// .lock()
// .unwrap() fn check_operator_set_cache(cache_key: &PermutationCacheKey) -> Option<HashSet<Vec<Op>>> {
// .insert(cache_key, cache_value); OPERATOR_SET_CACHE.lock().unwrap().get(cache_key).cloned()
// } }
fn store_operator_set_in_cache(cache_key: PermutationCacheKey, cache_value: HashSet<Vec<Op>>) {
OPERATOR_SET_CACHE
.lock()
.unwrap()
.insert(cache_key, cache_value);
}
// fn create_operator_set_recurse( // fn create_operator_set_recurse(
// operators_permutation_set: &mut HashSet<Vec<Op>>, // operators_permutation_set: &mut HashSet<Vec<Op>>,
@@ -79,7 +94,11 @@ fn create_permutations_recurse(
} }
pub fn create_permutations(set: HashSet<Op>, len: usize) -> HashSet<Vec<Op>> { pub fn create_permutations(set: HashSet<Op>, len: usize) -> HashSet<Vec<Op>> {
if let Some(cached_value) = check_operator_set_cache(&PermutationCacheKey::new(len, &set)) {
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(PermutationCacheKey::new(len, &set), permutations.clone());
permutations permutations
} }