From 80aa9615510db438e9920ca60d682271cf6a92fb Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Thu, 25 Sep 2025 11:38:33 +0200 Subject: [PATCH] Draft full solver --- src/cli.rs | 25 ++++++++++++++ src/main.rs | 34 +++++++++++------- src/solver.rs | 71 ++++++++++++++++++++++++++------------ src/word_representation.rs | 14 ++++++-- 4 files changed, 107 insertions(+), 37 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 44cb00d..3c6eb2c 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,3 +1,5 @@ +use std::fmt::Display; + use clap::{Parser, Subcommand, ValueEnum}; #[derive(Parser, Debug)] @@ -35,6 +37,10 @@ pub(crate) enum Commands { /// How many branches to try, warning exponential solving time #[arg(short, long, default_value_t = 5)] depth: usize, + + /// Solving algorithm + #[arg(short, long, default_value_t = AlgoType::Best)] + algo: AlgoType, }, } @@ -49,3 +55,22 @@ pub(crate) enum Wordtypes { // /// Mixed word list, picks a simpler list for weights and full for coverage // Mixed, } + +/// The word list to use +#[derive(ValueEnum, Debug, Clone)] +pub(crate) enum AlgoType { + /// Best candidate algorithm + Best, + + /// Full candidate list + Full, +} + +impl Display for AlgoType { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + AlgoType::Best => f.write_str("Best"), + AlgoType::Full => f.write_str("Full"), + } + } +} diff --git a/src/main.rs b/src/main.rs index 0d9036d..83d3a9e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,8 +29,12 @@ fn main_dispatch(args: Args) { } => { dispatch_solve(wl, contains, pos, not_contains); } - cli::Commands::List { number, depth } => { - dispatch_calculate(wl, weights, number, depth); + cli::Commands::List { + number, + depth, + algo, + } => { + dispatch_calculate(wl, weights, number, depth, algo); } } } @@ -77,19 +81,23 @@ fn dispatch_calculate( weights: word_representation::Weightlist, number: Option, depth: usize, + algo: cli::AlgoType, ) { let number = number.unwrap_or(3); - println!( - "Best {}: {:#?}", - number, - backtrack_best( - &wl, - &weights, - TotalScore::default(), - word_representation::NumberDepth::new(number), - word_representation::GuessDepth::new(depth) - ) - ); + match algo { + cli::AlgoType::Best => println!( + "Best {}: {:#?}", + number, + backtrack_best( + &wl, + &weights, + TotalScore::default(), + word_representation::NumberDepth::new(number), + word_representation::GuessDepth::new(depth) + ) + ), + cli::AlgoType::Full => todo!(), + } } fn main() { diff --git a/src/solver.rs b/src/solver.rs index d64895b..eda23ee 100644 --- a/src/solver.rs +++ b/src/solver.rs @@ -1,4 +1,4 @@ -use crate::word_representation::{self, GuessDepth, NumberDepth, TotalScore, Word, Wordlist}; +use crate::word_representation::{self, GuessDepth, NumberDepth, TotalScore}; pub(crate) fn backtrack_best( origin_list: &word_representation::Wordlist, @@ -44,7 +44,7 @@ pub(crate) fn backtrack_best( // println!("List guesses {:#?}", list_guesses); let best_implied_score = list_guesses.last().unwrap().clone(); // println!("best impl: {:?}", best_implied_score); - total_score.add(best_implied_score); + total_score.add_score(best_implied_score); // println!("best added: {:?}", total_score); total_score } @@ -53,38 +53,65 @@ pub(crate) fn backtrack_best( /// Create a list of best word combinations #[allow(unused_variables, dead_code)] pub(crate) fn create_list_best( - origin_list: word_representation::Wordlist, - origin_weights: &word_representation::Weightlist, - number_depth: NumberDepth, - guess_depth: GuessDepth, -) -> word_representation::TotalScore { - // Throw a parallel recursion over every possible combination - recursive_list(origin_list, origin_weights, number_depth, guess_depth); - // Collect and show sorted results - todo!() -} - -fn recursive_list( mut origin_list: word_representation::Wordlist, origin_weights: &word_representation::Weightlist, number_depth: NumberDepth, guess_depth: GuessDepth, ) -> Vec { + // Create the initial list to be consumed iteration after iteration origin_list.sort(); + + // Create an initial list let mut list = origin_list .iter() + .rev() + .take(*guess_depth) .map(|w| TotalScore::from_word(w.clone(), origin_weights.clone())) .collect::>(); - for letter in 0..*number_depth { - for guess in 0..*guess_depth { - // Make all cross guesses for previous entries - // Pick the nth best word - // let w = () - // Pick - } + // Throw a parallel recursion over every possible combination upgrading the list + if *number_depth > 1 { + list = recursive_list(origin_list, list, number_depth.decrement(), guess_depth); + } + + // Collect and show sorted results + list +} + +/// Recursively consume the list to add every word +fn recursive_list( + mut origin_list: word_representation::Wordlist, + consumed_list: Vec, + number_depth: NumberDepth, + guess_depth: GuessDepth, +) -> Vec { + // Iterate on initial list while consuming it + if *number_depth == 0 { + consumed_list + } else { + recursive_list( + origin_list.clone(), + consumed_list + .into_iter() + .flat_map(|sc| -> Vec { + origin_list.update_weights(&sc.weight); + origin_list.sort(); + origin_list + .iter() + .rev() + .take(*guess_depth) + .map(|w| { + let mut list = sc.clone(); + list.add_word(w.clone()); + list + }) + .collect() + }) + .collect::>(), + number_depth.decrement(), + guess_depth, + ) } - todo!() } // fn recursive_list( diff --git a/src/word_representation.rs b/src/word_representation.rs index 4d3e84c..5f6b0c3 100644 --- a/src/word_representation.rs +++ b/src/word_representation.rs @@ -167,6 +167,8 @@ pub(crate) struct TotalScore { pub(crate) words: Vec, /// The current weightlist associated with the solution pub(crate) weight: Weightlist, + // /// The wordlist associated with this solution + // pub(crate) wordl: Wordlist, } impl TotalScore { @@ -179,7 +181,8 @@ impl TotalScore { } } // New from word - pub(crate) fn from_word(word: Word, weight: Weightlist) -> Self { + pub(crate) fn from_word(word: Word, mut weight: Weightlist) -> Self { + weight.remove_word(&word); TotalScore { words: vec![word], score: 0, @@ -188,10 +191,17 @@ impl TotalScore { } /// Add the score to the the wordlist - pub(crate) fn add(&mut self, mut best_implied_score: TotalScore) { + pub(crate) fn add_score(&mut self, mut best_implied_score: TotalScore) { self.score += best_implied_score.score; self.words.append(&mut best_implied_score.words); } + + /// Add a word to the score + pub(crate) fn add_word(&mut self, word: Word) { + self.score += word.weight; + // self.weight.remove_word(&word); + self.words.push(word); + } } impl Ord for TotalScore {