More solver

This commit is contained in:
Dandaedre 2025-09-24 17:35:48 +02:00
parent b486a027c9
commit 3ac20f3257
4 changed files with 124 additions and 14 deletions

View File

@ -1,10 +1,13 @@
use clap::{Parser, Subcommand};
use clap::{Parser, Subcommand, ValueEnum};
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
pub(crate) struct Args {
#[command(subcommand)]
pub(crate) command: Commands,
#[arg(long, short, value_enum, default_value_t=Wordtypes::Full)]
pub(crate) wordlist: Wordtypes,
}
#[derive(Subcommand, Debug)]
@ -24,7 +27,7 @@ pub(crate) enum Commands {
out: Option<String>,
},
/// Tools to calculate best words for a wordle
Calculate {
List {
/// Number of guesses targetted
#[arg(short, long)]
number: Option<usize>,
@ -34,3 +37,15 @@ pub(crate) enum Commands {
depth: usize,
},
}
/// The word list to use
#[derive(ValueEnum, Debug, Clone)]
pub(crate) enum Wordtypes {
/// A simplified list of words, more likely to appear in the wordle
Simple,
/// The full list of words legal in wordle
Full,
// /// Mixed word list, picks a simpler list for weights and full for coverage
// Mixed,
}

View File

@ -14,7 +14,11 @@ mod solver;
mod word_representation;
fn main_dispatch(args: Args) {
let wl = word_representation::Wordlist::new(word_representation::load_words());
let wl = word_representation::Wordlist::new(match args.wordlist {
cli::Wordtypes::Simple => word_representation::load_easy_words(),
cli::Wordtypes::Full => word_representation::load_full_words(),
// cli::Wordtypes::Mixed => word_representation::load_full_words(),
});
let weights = wl.make_weights();
match args.command {
@ -25,7 +29,7 @@ fn main_dispatch(args: Args) {
} => {
dispatch_solve(wl, contains, pos, not_contains);
}
cli::Commands::Calculate { number, depth } => {
cli::Commands::List { number, depth } => {
dispatch_calculate(wl, weights, number, depth);
}
}

View File

@ -1,4 +1,4 @@
use crate::word_representation::{self, GuessDepth, NumberDepth};
use crate::word_representation::{self, GuessDepth, NumberDepth, TotalScore, Word, Wordlist};
pub(crate) fn backtrack_best(
origin_list: &word_representation::Wordlist,
@ -34,10 +34,11 @@ pub(crate) fn backtrack_best(
);
let mut v = bb.words.clone();
v.push(guess.clone());
list_guesses.push(word_representation::TotalScore {
score: bb.score + guess.weight,
words: v,
});
list_guesses.push(word_representation::TotalScore::new(
bb.score + guess.weight,
v,
weights,
));
}
list_guesses.sort();
// println!("List guesses {:#?}", list_guesses);
@ -52,10 +53,61 @@ 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_list: word_representation::Wordlist,
origin_weights: &word_representation::Weightlist,
letter_depth: usize,
guess_depth: usize,
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<word_representation::TotalScore> {
origin_list.sort();
let mut list = origin_list
.iter()
.map(|w| TotalScore::from_word(w.clone(), origin_weights.clone()))
.collect::<Vec<TotalScore>>();
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
}
}
todo!()
}
// 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

@ -41,7 +41,7 @@ impl PartialOrd for Word {
}
}
#[derive(Debug, Clone)]
#[derive(Default, Eq, PartialEq, Debug, Clone)]
pub(crate) struct Weightlist {
pub(crate) weights: Vec<isize>,
}
@ -78,13 +78,23 @@ impl Weightlist {
}
}
pub(crate) fn load_words() -> Vec<Word> {
/// Load the full list of 5 letters english words
pub(crate) fn load_full_words() -> Vec<Word> {
String::from_utf8_lossy(include_bytes!("../wordlists/final.txt"))
.split("\n")
.map(|s| s.into())
.collect()
}
/// Load a simplified list of 5 letters english words
pub(crate) fn load_easy_words() -> Vec<Word> {
String::from_utf8_lossy(include_bytes!("../wordlists/words.txt"))
.split("\n")
.map(|s| s.into())
.collect()
}
/// A full word list
#[derive(Debug, Clone)]
pub(crate) struct Wordlist {
pub(crate) inner: Vec<Word>,
@ -111,10 +121,14 @@ impl Display for Wordlist {
}
impl Wordlist {
/// Create a new word list from a vector of words
pub(crate) fn new(inner: Vec<Word>) -> Self {
Self { inner }
}
/// Create a weightlist from the wordlist
///
/// Calling this method DOES NOT update the Weight of words
pub(crate) fn make_weights(&self) -> Weightlist {
let mut weights = Weightlist::new();
self.inner.iter().for_each(|w| {
@ -129,14 +143,17 @@ impl Wordlist {
weights
}
/// Sort the list of words (according to their weight)
pub(crate) fn sort(&mut self) {
self.inner.sort();
}
/// Update the inner weight of every word depending on the weightlist
pub(crate) fn update_weights(&mut self, weights: &Weightlist) {
self.inner.iter_mut().for_each(|w| w.set_weight(weights));
}
/// Give the nth best word according to their weight
pub(crate) fn guess(&self, arg: usize) -> Word {
self.inner.iter().nth_back(arg).unwrap().clone()
}
@ -144,11 +161,33 @@ impl Wordlist {
#[derive(Default, Debug, Eq, PartialEq, Clone)]
pub(crate) struct TotalScore {
/// The given total weightscore
pub(crate) score: isize,
/// The list of selected words
pub(crate) words: Vec<Word>,
/// The current weightlist associated with the solution
pub(crate) weight: Weightlist,
}
impl TotalScore {
// New Totalscore
pub(crate) fn new(score: isize, words: Vec<Word>, weight: Weightlist) -> Self {
Self {
score,
words,
weight,
}
}
// New from word
pub(crate) fn from_word(word: Word, weight: Weightlist) -> Self {
TotalScore {
words: vec![word],
score: 0,
weight,
}
}
/// Add the score to the the wordlist
pub(crate) fn add(&mut self, mut best_implied_score: TotalScore) {
self.score += best_implied_score.score;
self.words.append(&mut best_implied_score.words);