From 0abb5d1d627a692ec7d646ad003a0d675067b71c Mon Sep 17 00:00:00 2001 From: Matthieu Jolimaitre Date: Sat, 26 Oct 2024 16:49:29 +0200 Subject: [PATCH] update grouping solver avg ranking and add biggest ranking --- loop.ts | 2 +- src/game/simulation.rs | 2 ++ src/proxy.rs | 2 +- src/solve/grouping.rs | 52 +++++++++++++++++++++++++++++++----------- 4 files changed, 43 insertions(+), 15 deletions(-) diff --git a/loop.ts b/loop.ts index 8f48d94..37573ed 100755 --- a/loop.ts +++ b/loop.ts @@ -24,7 +24,7 @@ async function main() { } function run() { - const [cmd, ...args] = ["cargo", "run", "--release"]; + const [cmd, ...args] = ["cargo", "run", "--release", "--bin=proxy"]; return new Deno.Command(cmd, { args, stdin: "inherit", stderr: "null" }) .spawn(); } diff --git a/src/game/simulation.rs b/src/game/simulation.rs index 05d0b98..178d560 100644 --- a/src/game/simulation.rs +++ b/src/game/simulation.rs @@ -42,7 +42,9 @@ pub fn wordle_buffs_for(target: &str) -> (Vec>, Vec>, Vec>, Vec)) { + #[inline] fn to_info(letter: &mut Option, info: &mut Option, kind: InfoKind) { *letter = None; info.insert(kind); diff --git a/src/proxy.rs b/src/proxy.rs index a453f55..2360fbd 100644 --- a/src/proxy.rs +++ b/src/proxy.rs @@ -15,6 +15,6 @@ pub fn main() { let dict = dictionnary::gutenberg(infos.len()); let mut solver = Grouping::new(dict.into_iter().filter(|w| matches(w, &infos))); solver.learn(infos); - let result = game.play_all(solver, Some(5)); + let result = game.play_all(solver, Some(6)); println!("{result:?}"); } diff --git a/src/solve/grouping.rs b/src/solve/grouping.rs index c7d1b4b..d69b290 100644 --- a/src/solve/grouping.rs +++ b/src/solve/grouping.rs @@ -30,7 +30,7 @@ impl Grouping { } /// Gives a score to the word (lowest is best). - fn rank_word(&self, word: &str) -> usize { + fn rank_word_avg_groups(&self, word: &str) -> (usize, usize) { let mut groups = [0; 3]; let mut buffs = wordle_buffs_for(word); for target in &self.candidates { @@ -46,7 +46,28 @@ impl Grouping { } let min = groups.iter().min().unwrap(); let max = groups.iter().max().unwrap(); - (max - min) + let there_count = groups[2]; + (max - min, there_count) + } + + fn rank_word_biggest_group(&self, word: &str) -> (usize, usize) { + let mut there_count = 0; + let mut biggest = 0; + let mut buffs = wordle_buffs_for(word); + for target in &self.candidates { + let mut groups = [0; 3]; + wordle_inner(target, word, &mut buffs); + let infos = &buffs.2; + for i in infos { + match i.kind { + InfoKind::Abscent => groups[0] += 1, + InfoKind::Elsewhere => groups[1] += 1, + InfoKind::There => (groups[2] += 1, there_count += 1).0, + } + } + biggest = biggest.max(groups.into_iter().max().unwrap_or_default()); + } + (biggest, there_count) } } @@ -56,12 +77,19 @@ impl Solver for Grouping { return self.candidates.first().cloned(); } - self.dict + let scored = self + .dict .iter() - .take(10_000) + .take(100_000) .par_bridge() - .map(|word| (word, self.rank_word(word))) - .min_by_key(|(_, score)| *score) + .map(|word| (word, self.rank_word_avg_groups(word))) + .collect::>(); + + let best_score = scored.iter().map(|(_, (s, _))| *s).min()?; + scored + .into_iter() + .filter(|(_, (s, _))| *s == best_score) + .max_by_key(|(_, (_, t))| *t) .map(|(w, _)| w.to_string()) .or_else(|| self.candidates.first().cloned()) } @@ -77,8 +105,6 @@ impl Solver for Grouping { self.dict.remove(index); } self.candidates = self.candidates.drain(..).filter(|w| matches(w, &infos)).collect(); - dbg!(&self.candidates); - dbg!(self.candidates.len()); } } @@ -121,8 +147,8 @@ fn test_misc() { // assert!(grouping.rank_word("coton") > grouping.rank_word("abaca")); let grouping = Grouping::new(["abregee", "amorcee", "marquee", "assuree", "separee"]); - dbg!(grouping.rank_word("volitif")); - dbg!(grouping.rank_word("amorcee")); + dbg!(grouping.rank_word_avg_groups("volitif")); + dbg!(grouping.rank_word_avg_groups("amorcee")); let mut gut = gutenberg(7); dbg!(gut.len()); @@ -158,9 +184,9 @@ fn test_misc() { character: 't', }, ]); - dbg!(grouping.rank_word("jouxtee")); - dbg!(grouping.rank_word("jutions")); - dbg!(grouping.rank_word("jaspine")); + dbg!(grouping.rank_word_avg_groups("jouxtee")); + dbg!(grouping.rank_word_avg_groups("jutions")); + dbg!(grouping.rank_word_avg_groups("jaspine")); dbg!(grouping.guess()); }