From f235c46bd472e0013fd6a379fa2c7b4beed69c4d Mon Sep 17 00:00:00 2001 From: Bearmine Date: Mon, 16 Jun 2025 13:32:26 -0500 Subject: [PATCH] Add caching back in --- day_07/src/equation.rs | 4 +-- day_07/src/equation/operator_set.rs | 49 ++++++++++++++++++++--------- 2 files changed, 36 insertions(+), 17 deletions(-) diff --git a/day_07/src/equation.rs b/day_07/src/equation.rs index 2549c3f..0c6914c 100644 --- a/day_07/src/equation.rs +++ b/day_07/src/equation.rs @@ -55,7 +55,7 @@ impl Equation { match op { Op::Multiply => accumulator *= self.components[i + 1], Op::Add => accumulator += self.components[i + 1], - Op::CONCAT => { + Op::Concat => { let accumulator_string = format!("{}", accumulator); let component_string = format!("{}", self.components[i + 1]); accumulator = (accumulator_string + &component_string).parse().unwrap(); @@ -82,7 +82,7 @@ impl Equation { pub fn solvable_multiply_add_concat(&self) -> bool { 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, ); diff --git a/day_07/src/equation/operator_set.rs b/day_07/src/equation/operator_set.rs index f9a5a1c..a67b697 100644 --- a/day_07/src/equation/operator_set.rs +++ b/day_07/src/equation/operator_set.rs @@ -4,27 +4,42 @@ use std::collections::HashSet; pub enum Op { Multiply, Add, - CONCAT, + Concat, } -// use std::collections::HashMap; -// use std::sync::{LazyLock, Mutex}; +#[derive(Debug, Clone, Hash, PartialEq, Eq)] +struct PermutationCacheKey { + size: usize, + set: Vec, +} -// type OperatorCache = HashMap>>; +impl PermutationCacheKey { + fn new(size: usize, set: &HashSet) -> PermutationCacheKey { + PermutationCacheKey { + size, + set: set.iter().cloned().collect(), + } + } +} -// static OPERATOR_SET_CACHE: LazyLock> = -// LazyLock::new(|| Mutex::new(HashMap::new())); +use std::collections::HashMap; +use std::sync::{LazyLock, Mutex}; -// fn check_operator_set_cache(cache_key: usize) -> Option>> { -// OPERATOR_SET_CACHE.lock().unwrap().get(&cache_key).cloned() -// } +type OperatorCache = HashMap>>; -// fn store_operator_set_in_cache(cache_key: usize, cache_value: HashSet>) { -// OPERATOR_SET_CACHE -// .lock() -// .unwrap() -// .insert(cache_key, cache_value); -// } +static OPERATOR_SET_CACHE: LazyLock> = + LazyLock::new(|| Mutex::new(HashMap::new())); + +fn check_operator_set_cache(cache_key: &PermutationCacheKey) -> Option>> { + OPERATOR_SET_CACHE.lock().unwrap().get(cache_key).cloned() +} + +fn store_operator_set_in_cache(cache_key: PermutationCacheKey, cache_value: HashSet>) { + OPERATOR_SET_CACHE + .lock() + .unwrap() + .insert(cache_key, cache_value); +} // fn create_operator_set_recurse( // operators_permutation_set: &mut HashSet>, @@ -79,7 +94,11 @@ fn create_permutations_recurse( } pub fn create_permutations(set: HashSet, len: usize) -> HashSet> { + if let Some(cached_value) = check_operator_set_cache(&PermutationCacheKey::new(len, &set)) { + return cached_value; + } let mut permutations = HashSet::new(); create_permutations_recurse(&set, len, &mut permutations, vec![]); + store_operator_set_in_cache(PermutationCacheKey::new(len, &set), permutations.clone()); permutations }