From b486a027c960fcbdfce30f92f91a7eb08be9cc72 Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Wed, 24 Sep 2025 11:21:18 +0200 Subject: [PATCH] More rework --- src/main.rs | 8 +++++- src/solver.rs | 23 ++++++++++++----- src/word_representation.rs | 52 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7157aa0..0d16266 100644 --- a/src/main.rs +++ b/src/main.rs @@ -78,7 +78,13 @@ fn dispatch_calculate( println!( "Best {}: {:#?}", number, - backtrack_best(&wl, &weights, TotalScore::default(), number, depth) + backtrack_best( + &wl, + &weights, + TotalScore::default(), + word_representation::NumberDepth::new(number), + word_representation::GuessDepth::new(depth) + ) ); } diff --git a/src/solver.rs b/src/solver.rs index b6e7338..9ec232a 100644 --- a/src/solver.rs +++ b/src/solver.rs @@ -1,13 +1,13 @@ -use crate::word_representation; +use crate::word_representation::{self, GuessDepth, NumberDepth}; pub(crate) fn backtrack_best( origin_list: &word_representation::Wordlist, origin_weights: &word_representation::Weightlist, mut total_score: word_representation::TotalScore, - letter_depth: usize, - guess_depth: usize, + number_depth: NumberDepth, + guess_depth: GuessDepth, ) -> word_representation::TotalScore { - if letter_depth == 0 { + if *number_depth == 0 { word_representation::TotalScore::default() } else { // println!("Starts new track: TS[{:?}]", &total_score); @@ -19,7 +19,7 @@ pub(crate) fn backtrack_best( // Pick n bests word, backtrack their score and return the best of the best results let mut list_guesses = Vec::new(); - for i in 0..guess_depth { + for i in 0..*guess_depth { let guess = list.guess(i); // println!("Guess: {:?}", guess); let mut weights = origin_weights.clone(); @@ -29,7 +29,7 @@ pub(crate) fn backtrack_best( &list, &weights, total_score.clone(), - letter_depth - 1, + number_depth.decrement(), guess_depth, ); let mut v = bb.words.clone(); @@ -48,3 +48,14 @@ pub(crate) fn backtrack_best( total_score } } + +/// 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, + letter_depth: usize, + guess_depth: usize, +) -> word_representation::TotalScore { + todo!() +} diff --git a/src/word_representation.rs b/src/word_representation.rs index b0394ca..74b4399 100644 --- a/src/word_representation.rs +++ b/src/word_representation.rs @@ -90,6 +90,14 @@ pub(crate) struct Wordlist { pub(crate) inner: Vec, } +impl std::ops::Deref for Wordlist { + type Target = Vec; + + fn deref(&self) -> &Self::Target { + &self.inner + } +} + impl Display for Wordlist { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let o = self @@ -167,3 +175,47 @@ impl PartialOrd for TotalScore { Some(self.cmp(other)) } } + +/// The number of different combinations to try +#[derive(Clone, Copy)] +pub(crate) struct GuessDepth { + n: usize, +} + +impl GuessDepth { + pub(crate) fn new(n: usize) -> Self { + Self { n } + } +} + +impl std::ops::Deref for GuessDepth { + type Target = usize; + + fn deref(&self) -> &Self::Target { + &self.n + } +} + +/// The number of words to generate in the solution +#[derive(Clone, Copy)] +pub(crate) struct NumberDepth { + n: usize, +} +impl NumberDepth { + pub(crate) fn new(n: usize) -> Self { + Self { n } + } + + #[inline] + pub(crate) fn decrement(&self) -> NumberDepth { + NumberDepth::new(self.n - 1) + } +} + +impl std::ops::Deref for NumberDepth { + type Target = usize; + + fn deref(&self) -> &Self::Target { + &self.n + } +}