From 41676e9a233d0636d5bf8a330efa72c13e399134 Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Fri, 22 Mar 2024 22:10:10 +0100 Subject: [PATCH] naive filters matches we can actually win some games in 6 steps with this --- src/game/mod.rs | 6 +++++- src/game/response.rs | 2 +- src/solve/naive/mod.rs | 14 +++++++++----- src/wlist/word.rs | 2 +- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/game/mod.rs b/src/game/mod.rs index f8ebcaa..9e96fcd 100644 --- a/src/game/mod.rs +++ b/src/game/mod.rs @@ -1,5 +1,5 @@ use crate::error::*; -use crate::wlist::word::{Word, WordData}; +use crate::wlist::word::{ManyWordDatas, ManyWordsRef, Word, WordData}; use crate::wlist::WordList; use libpt::log::debug; @@ -139,6 +139,10 @@ impl<'wl, WL: WordList> Game<'wl, WL> { pub fn wordlist(&self) -> &WL { self.wordlist } + + pub(crate) fn made_guesses(&self) -> ManyWordsRef { + self.responses.iter().map(|r| r.guess()).collect() + } } /// Build and Configure a [`Game`] diff --git a/src/game/response.rs b/src/game/response.rs index 6966b87..2830e22 100644 --- a/src/game/response.rs +++ b/src/game/response.rs @@ -67,7 +67,7 @@ impl GuessResponse { &self.evaluation } - pub fn guess(&self) -> &str { + pub fn guess(&self) -> &Word { &self.guess } } diff --git a/src/solve/naive/mod.rs b/src/solve/naive/mod.rs index bc2f9e3..13503cc 100644 --- a/src/solve/naive/mod.rs +++ b/src/solve/naive/mod.rs @@ -1,6 +1,6 @@ use libpt::log::info; -use crate::wlist::word::Word; +use crate::wlist::word::{ManyWordDatas, Word}; use crate::wlist::WordList; use super::{AnyBuiltinSolver, Solver, Status}; @@ -26,11 +26,15 @@ impl<'wl, WL: WordList> Solver<'wl, WL> for NaiveSolver<'wl, WL> { } } } - game.wordlist() + let matches: ManyWordDatas = game + .wordlist() .get_words_matching(buf) - .expect("the solution does not exist in the wordlist")[0] - .0 - .clone() + .expect("the solution does not exist in the wordlist") + .iter() + .filter(|m| !game.made_guesses().contains(&&m.0)) + .map(|v| v.to_owned()) + .collect(); + matches[0].0.to_owned() } } diff --git a/src/wlist/word.rs b/src/wlist/word.rs index 91128d4..abb848e 100644 --- a/src/wlist/word.rs +++ b/src/wlist/word.rs @@ -11,7 +11,7 @@ pub type Frequency = f64; // PERF: Hash for String is probably a bottleneck pub type Word = String; pub type WordData = (Word, Frequency); -pub type ManyWords<'a> = Vec<&'a Word>; +pub type ManyWordsRef<'a> = Vec<&'a Word>; pub type ManyWordDatas = Vec<(Word, Frequency)>; #[derive(Clone)]