First commit

This commit is contained in:
Dandaedre 2025-09-11 17:12:20 +02:00
commit 6135bd5909
4 changed files with 5982 additions and 0 deletions

16
.gitignore vendored Normal file
View File

@ -0,0 +1,16 @@
# ---> Rust
# Generated by Cargo
# will have compiled files and executables
debug/
target/
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock
# These are backup files generated by rustfmt
**/*.rs.bk
# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb

6
Cargo.toml Normal file
View File

@ -0,0 +1,6 @@
[package]
name = "wodol"
version = "0.1.0"
edition = "2024"
[dependencies]

5757
sgb-words.txt Normal file

File diff suppressed because it is too large Load Diff

203
src/main.rs Normal file
View File

@ -0,0 +1,203 @@
// Load list
// Struct record plus hash ?
// Letter frequency as weight
// find better weight
#![allow(dead_code)]
use std::fs::read_to_string;
#[derive(Debug, Eq, PartialEq, Clone)]
struct Word {
origin: String,
// hash: u64,
weight: isize,
}
impl Word {
fn set_weight(&mut self, weights: &Weightlist) {
self.weight = 0;
let mut found = vec![];
self.origin.chars().for_each(|c| {
if !found.contains(&c) {
self.weight += weights.getchar(c);
found.push(c);
}
});
}
}
impl From<&str> for Word {
fn from(value: &str) -> Self {
Self {
origin: value.to_owned(),
weight: 0,
}
}
}
impl Ord for Word {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.weight.cmp(&other.weight)
}
}
impl PartialOrd for Word {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
#[derive(Debug, Clone)]
struct Weightlist {
weights: Vec<isize>,
}
impl Weightlist {
fn new() -> Self {
Self {
weights: vec![0; 26],
}
}
fn addweight(&mut self, c: char) {
self.weights[c as usize - 'a' as usize] += 1;
}
fn setweight(&mut self, c: char, val: isize) {
self.weights[c as usize - 'a' as usize] = val;
}
fn getid(&self, id: usize) -> isize {
self.weights[id]
}
fn getchar(&self, c: char) -> isize {
self.weights[c as usize - 'a' as usize]
}
fn remove_word(&mut self, guess: &Word) {
let mut found = vec![];
for letter in guess.origin.chars() {
if !found.contains(&letter) {
self.setweight(letter, -200);
found.push(letter)
}
}
}
}
fn read_list(path: &str) -> Vec<Word> {
read_to_string(path)
.unwrap()
.split("\n")
.map(|s| s.into())
.collect()
}
#[derive(Debug, Clone)]
struct Wordlist {
inner: Vec<Word>,
}
impl Wordlist {
fn new(inner: Vec<Word>) -> Self {
Self { inner }
}
fn make_weights(&self) -> Weightlist {
let mut weights = Weightlist::new();
self.inner.iter().for_each(|w| {
w.origin.chars().for_each(|c| weights.addweight(c));
});
weights
}
fn sort(&mut self) {
self.inner.sort();
}
fn update_weights(&mut self, weights: &Weightlist) {
self.inner.iter_mut().for_each(|w| w.set_weight(weights));
}
fn guess(&self, arg: usize) -> Word {
self.inner.iter().nth_back(arg).unwrap().clone()
}
}
#[derive(Default, Debug, Eq, PartialEq, Clone)]
struct TotalScore {
score: isize,
words: Vec<Word>,
}
impl TotalScore {
fn add(&mut self, mut best_implied_score: TotalScore) {
self.score += best_implied_score.score;
self.words.append(&mut best_implied_score.words);
}
}
impl Ord for TotalScore {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.score.cmp(&other.score)
}
}
impl PartialOrd for TotalScore {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
fn backtrack_best(
origin_list: &Wordlist,
origin_weights: &Weightlist,
mut total_score: TotalScore,
depth: usize,
) -> TotalScore {
if depth == 0 {
TotalScore::default()
} else {
// Sort original data
let mut list = origin_list.clone();
let mut 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 {
let guess = list.guess(i);
// println!("{:?}", guess);
let mut weights = origin_weights.clone();
weights.remove_word(&guess);
// Backtrack the implied scores
let bb = backtrack_best(&list, &weights, total_score.clone(), depth - 1);
let mut v = bb.words.clone();
v.push(guess.clone());
list_guesses.push(TotalScore {
score: bb.score + guess.weight,
words: v,
});
}
list_guesses.sort();
let best_implied_score = list_guesses.last().unwrap().clone();
// println!("best impl: {:?}", best_implied_score);
total_score.add(best_implied_score);
// println!("best added: {:?}", total_score);
total_score
}
}
fn main() {
let wl = Wordlist::new(read_list("./sgb-words.txt"));
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);
}