Solver, no clap tho

This commit is contained in:
Dandaedre 2025-09-11 18:10:07 +02:00
parent 6135bd5909
commit f9fd2834f1
2 changed files with 53 additions and 19 deletions

View File

@ -2,5 +2,3 @@
name = "wodol"
version = "0.1.0"
edition = "2024"
[dependencies]

View File

@ -4,7 +4,7 @@
// find better weight
#![allow(dead_code)]
use std::fs::read_to_string;
use std::{fmt::Display, fs::read_to_string};
#[derive(Debug, Eq, PartialEq, Clone)]
struct Word {
@ -94,6 +94,17 @@ struct Wordlist {
inner: Vec<Word>,
}
impl Display for Wordlist {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let o = self
.inner
.iter()
.map(|w| w.origin.clone())
.collect::<Vec<String>>()
.join(", ");
write!(f, "{}", o)
}
}
impl Wordlist {
fn new(inner: Vec<Word>) -> Self {
Self { inner }
@ -133,7 +144,16 @@ impl TotalScore {
}
impl Ord for TotalScore {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.score.cmp(&other.score)
match self.score.cmp(&other.score) {
std::cmp::Ordering::Equal => {
if let (Some(w1), Some(w2)) = (self.words.first(), other.words.first()) {
w1.cmp(w2)
} else {
std::cmp::Ordering::Equal
}
}
a => a,
}
}
}
@ -154,13 +174,13 @@ fn backtrack_best(
} else {
// Sort original data
let mut list = origin_list.clone();
let mut weights = origin_weights.clone();
let weights = origin_weights.clone();
list.update_weights(&weights);
list.sort();
// 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..5 {
for i in 0..10 {
let guess = list.guess(i);
// println!("{:?}", guess);
let mut weights = origin_weights.clone();
@ -187,17 +207,33 @@ fn main() {
let weights = wl.make_weights();
// println!("{:?}", wl);
let best = backtrack_best(&wl, &weights, TotalScore::default(), 3);
println!("Best: {:?}", best);
let list: Vec<&Word> = wl
.inner
.iter()
.filter(|w| {
String::from("ware")
.chars()
.into_iter()
.all(|c| w.origin.contains(c))
})
.collect();
println!("{:?}", list);
// let best = backtrack_best(&wl, &weights, TotalScore::default(), 5);
// println!(
// "Best 5: {:?}",
// backtrack_best(&wl, &weights, TotalScore::default(), 5)
// );
// println!(
// "Best 4: {:?}",
// backtrack_best(&wl, &weights, TotalScore::default(), 4)
// );
println!(
"Best 3: {:#?}",
backtrack_best(&wl, &weights, TotalScore::default(), 3)
);
println!(
"Best 2: {:#?}",
backtrack_best(&wl, &weights, TotalScore::default(), 2)
);
let list = Wordlist::new(
wl.inner
.into_iter()
.filter(|w| {
String::from("olen")
.chars()
.into_iter()
.all(|c| w.origin.contains(c))
})
.collect(),
);
println!("{}", list);
}