Sorted this mess of list

This commit is contained in:
Dandaedre 2025-09-25 17:33:07 +02:00
parent 80aa961551
commit 305e06d5d4
3 changed files with 100 additions and 51 deletions

View File

@ -4,7 +4,7 @@
// find better weight
// #![allow(dead_code)]
use crate::cli::Args;
use crate::solver::backtrack_best;
use crate::solver::{backtrack_best, create_list_best};
use crate::word_representation::TotalScore;
use clap::Parser;
use regex::Regex;
@ -86,7 +86,7 @@ fn dispatch_calculate(
let number = number.unwrap_or(3);
match algo {
cli::AlgoType::Best => println!(
"Best {}: {:#?}",
"Best {}: {}",
number,
backtrack_best(
&wl,
@ -96,7 +96,17 @@ fn dispatch_calculate(
word_representation::GuessDepth::new(depth)
)
),
cli::AlgoType::Full => todo!(),
cli::AlgoType::Full => {
println!(
"Full list {}",
create_list_best(
wl,
&weights,
word_representation::NumberDepth::new(number),
word_representation::GuessDepth::new(depth)
)
)
}
}
}

View File

@ -1,4 +1,4 @@
use crate::word_representation::{self, GuessDepth, NumberDepth, TotalScore};
use crate::word_representation::{self, GuessDepth, ManyScores, NumberDepth, TotalScore};
pub(crate) fn backtrack_best(
origin_list: &word_representation::Wordlist,
@ -38,6 +38,7 @@ pub(crate) fn backtrack_best(
bb.score + guess.weight,
v,
weights,
list.clone(),
));
}
list_guesses.sort();
@ -51,14 +52,14 @@ pub(crate) fn backtrack_best(
}
/// Create a list of best word combinations
#[allow(unused_variables, dead_code)]
pub(crate) fn create_list_best(
mut origin_list: word_representation::Wordlist,
origin_weights: &word_representation::Weightlist,
number_depth: NumberDepth,
guess_depth: GuessDepth,
) -> Vec<word_representation::TotalScore> {
) -> ManyScores {
// Create the initial list to be consumed iteration after iteration
origin_list.update_weights(origin_weights);
origin_list.sort();
// Create an initial list
@ -66,21 +67,21 @@ pub(crate) fn create_list_best(
.iter()
.rev()
.take(*guess_depth)
.map(|w| TotalScore::from_word(w.clone(), origin_weights.clone()))
.map(|w| TotalScore::from_word(w.clone(), origin_weights.clone(), origin_list.clone()))
.collect::<Vec<TotalScore>>();
// Throw a parallel recursion over every possible combination upgrading the list
// Throw a recursion over every possible combination upgrading the list
if *number_depth > 1 {
list = recursive_list(origin_list, list, number_depth.decrement(), guess_depth);
list = recursive_list(list, number_depth.decrement(), guess_depth);
}
// Collect and show sorted results
list
list.sort();
ManyScores::new(list)
}
/// Recursively consume the list to add every word
fn recursive_list(
mut origin_list: word_representation::Wordlist,
consumed_list: Vec<TotalScore>,
number_depth: NumberDepth,
guess_depth: GuessDepth,
@ -90,13 +91,10 @@ fn recursive_list(
consumed_list
} else {
recursive_list(
origin_list.clone(),
consumed_list
.into_iter()
.flat_map(|sc| -> Vec<TotalScore> {
origin_list.update_weights(&sc.weight);
origin_list.sort();
origin_list
sc.wordlist
.iter()
.rev()
.take(*guess_depth)
@ -113,28 +111,3 @@ fn recursive_list(
)
}
}
// fn recursive_list(
// origin_list: &word_representation::Wordlist,
// origin_weights: &word_representation::Weightlist,
// number_depth: NumberDepth,
// guess_depth: GuessDepth,
// ) -> Vec<word_representation::TotalScore> {
// if *number_depth == 0 {
// Vec::new()
// } else {
// let mut list = Vec::with_capacity(*number_depth);
// for i in 0..*guess_depth {
// // Select one word and make it the new list
// //
// // Flatten all subsequent guess this could be a massive time sink tho
// list.append(&mut recursive_list(
// origin_list,
// origin_weights,
// number_depth.decrement(),
// guess_depth,
// ));
// }
// todo!()
// }
// }

View File

@ -71,7 +71,7 @@ impl Weightlist {
let mut found = vec![];
for letter in guess.origin.chars() {
if !found.contains(&letter) {
self.setweight(letter, -200);
self.setweight(letter, 0);
found.push(letter)
}
}
@ -95,11 +95,24 @@ pub(crate) fn load_easy_words() -> Vec<Word> {
}
/// A full word list
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub(crate) struct Wordlist {
pub(crate) inner: Vec<Word>,
}
// This skip is used to allow for faster comparisons
impl Ord for Wordlist {
fn cmp(&self, _other: &Self) -> std::cmp::Ordering {
std::cmp::Ordering::Equal
}
}
impl PartialOrd for Wordlist {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl std::ops::Deref for Wordlist {
type Target = Vec<Word>;
@ -166,40 +179,54 @@ pub(crate) struct TotalScore {
/// The list of selected words
pub(crate) words: Vec<Word>,
/// The current weightlist associated with the solution
pub(crate) weight: Weightlist,
// /// The wordlist associated with this solution
// pub(crate) wordl: Wordlist,
pub(crate) weights: Weightlist,
/// The wordlist associated with this solution
pub(crate) wordlist: Wordlist,
}
impl TotalScore {
// New Totalscore
pub(crate) fn new(score: isize, words: Vec<Word>, weight: Weightlist) -> Self {
pub(crate) fn new(
score: isize,
words: Vec<Word>,
weight: Weightlist,
wordlist: Wordlist,
) -> Self {
Self {
score,
words,
weight,
weights: weight,
wordlist,
}
}
// New from word
pub(crate) fn from_word(word: Word, mut weight: Weightlist) -> Self {
pub(crate) fn from_word(word: Word, mut weight: Weightlist, mut wordlist: Wordlist) -> Self {
let score = word.weight;
weight.remove_word(&word);
wordlist.update_weights(&weight);
wordlist.sort();
TotalScore {
words: vec![word],
score: 0,
weight,
score,
weights: weight,
wordlist,
}
}
/// Add the score to the the wordlist
// TODO: Remove word ?
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
// TODO: Remove word ?
pub(crate) fn add_word(&mut self, word: Word) {
self.score += word.weight;
// self.weight.remove_word(&word);
self.weights.remove_word(&word);
self.wordlist.update_weights(&self.weights);
self.wordlist.sort();
self.words.push(word);
}
}
@ -225,6 +252,45 @@ impl PartialOrd for TotalScore {
}
}
impl Display for TotalScore {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&format!("{}: {:#?}", self.score, self.words))
}
}
pub(crate) struct ManyScores {
inner: Vec<TotalScore>,
}
impl ManyScores {
pub(crate) fn new(list: Vec<TotalScore>) -> Self {
Self { inner: list }
}
}
impl std::ops::Deref for ManyScores {
type Target = Vec<TotalScore>;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl Display for ManyScores {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.iter().try_for_each(|sc| {
f.write_str(&format!(
"{}: {}\n",
sc.score,
sc.words
.iter()
.map(|w| w.origin.clone())
.collect::<Vec<String>>()
.join(", ")
))
})
}
}
/// The number of different combinations to try
#[derive(Clone, Copy)]
pub(crate) struct GuessDepth {