From 6cc6694747bddc59a5818e01e4d534e7001dc93f Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Sat, 3 Aug 2024 19:09:07 +0200 Subject: [PATCH] fix(naive): solver works again after evaluation bugfixes, but is dumber than before --- src/solve/naive/mod.rs | 22 ++++++++++------------ src/solve/naive/states.rs | 24 +++++++++++++++++++++++- 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/src/solve/naive/mod.rs b/src/solve/naive/mod.rs index e824dde..5a4b2d6 100644 --- a/src/solve/naive/mod.rs +++ b/src/solve/naive/mod.rs @@ -4,14 +4,15 @@ use libpt::log::{debug, error, info, trace}; use crate::error::{SolverError, WResult}; use crate::game::evaluation::{Evaluation, EvaluationUnit}; +use crate::game::response::Status; use crate::wlist::word::{Word, WordData}; use crate::wlist::WordList; -use super::{AnyBuiltinSolver, Solver, Status}; - mod states; use states::*; +use super::{AnyBuiltinSolver, Solver}; + #[derive(Debug, Clone)] pub struct NaiveSolver<'wl, WL> { wl: &'wl WL, @@ -37,9 +38,10 @@ impl<'wl, WL: WordList> Solver<'wl, WL> for NaiveSolver<'wl, WL> { let mut state: SolverState = SolverState::new(); let responses = game.responses().iter().enumerate(); for (_idx, response) in responses { - let mut already_found_amounts: HashMap = HashMap::new(); + let mut abs_freq: HashMap = HashMap::new(); let evaluation: &Evaluation = response.evaluation(); for (idx, p) in evaluation.clone().into_iter().enumerate() { + state.start_step(); match p.1 { Status::Matched => { pattern.replace_range(idx..idx + 1, &p.0.to_string()); @@ -49,7 +51,7 @@ impl<'wl, WL: WordList> Solver<'wl, WL> for NaiveSolver<'wl, WL> { .entry(p.0) .or_insert(CharInfo::new(game.length())) .found_at(idx); - *already_found_amounts.entry(p.0).or_default() += 1; + *abs_freq.entry(p.0).or_default() += 1; } Status::Exists => { let cinfo = state @@ -57,16 +59,12 @@ impl<'wl, WL: WordList> Solver<'wl, WL> for NaiveSolver<'wl, WL> { .entry(p.0) .or_insert(CharInfo::new(game.length())); cinfo.tried_but_failed(idx); - *already_found_amounts.entry(p.0).or_default() += 1; - cinfo.min_occurences(already_found_amounts[&p.0]); + *abs_freq.entry(p.0).or_default() += 1; } - Status::None => state - .char_map_mut() - .entry(p.0) - .or_insert(CharInfo::new(game.length())) - .max_occurences(*already_found_amounts.entry(p.0).or_default()), + Status::None => (), } - trace!("absolute frequencies: {already_found_amounts:?}"); + trace!("absolute frequencies: {abs_freq:?}"); + state.finish_step(&abs_freq); } } diff --git a/src/solve/naive/states.rs b/src/solve/naive/states.rs index 486d7e7..a385b84 100644 --- a/src/solve/naive/states.rs +++ b/src/solve/naive/states.rs @@ -37,7 +37,7 @@ impl SolverState { pub(crate) fn get_all_known_contained(&self) -> Vec<(&char, &CharInfo)> { self.char_map .iter() - .filter(|(key, value)| value.part_of_solution()) + .filter(|(_key, value)| value.part_of_solution()) .collect() } @@ -49,6 +49,28 @@ impl SolverState { } true } + + pub(crate) fn start_step(&mut self) {} + + pub(crate) fn finish_step(&mut self, abs_freq: &HashMap) { + for (k, v) in abs_freq { + if *v == 0 { + self.char_map + .get_mut(k) + .expect( + "char in abs_freq was not added to the char_map before finalizing the step", + ) + .max_occurences(0); + } else { + self.char_map + .get_mut(k) + .expect( + "char in abs_freq was not added to the char_map before finalizing the step", + ) + .min_occurences(*v); + } + } + } } impl CharInfo {