Draft full solver
This commit is contained in:
parent
3ac20f3257
commit
80aa961551
25
src/cli.rs
25
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"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
34
src/main.rs
34
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<usize>,
|
||||
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() {
|
||||
|
|
|
|||
|
|
@ -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<word_representation::TotalScore> {
|
||||
// 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::<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
|
||||
}
|
||||
// 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<TotalScore>,
|
||||
number_depth: NumberDepth,
|
||||
guess_depth: GuessDepth,
|
||||
) -> Vec<word_representation::TotalScore> {
|
||||
// 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<TotalScore> {
|
||||
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::<Vec<TotalScore>>(),
|
||||
number_depth.decrement(),
|
||||
guess_depth,
|
||||
)
|
||||
}
|
||||
todo!()
|
||||
}
|
||||
|
||||
// fn recursive_list(
|
||||
|
|
|
|||
|
|
@ -167,6 +167,8 @@ pub(crate) struct TotalScore {
|
|||
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,
|
||||
}
|
||||
|
||||
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 {
|
||||
|
|
|
|||
Reference in New Issue