This repository has been archived on 2026-09-13. You can view files and clone it, but cannot push or open issues or pull requests.
worldulu/src/word_representation.rs

170 lines
4.1 KiB
Rust

use std::fmt::Display;
#[derive(Debug, Eq, PartialEq, Clone)]
pub(crate) struct Word {
pub(crate) origin: String,
// hash: u64,
pub(crate) weight: isize,
}
impl Word {
pub(crate) 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)]
pub(crate) struct Weightlist {
pub(crate) weights: Vec<isize>,
}
impl Weightlist {
pub(crate) fn new() -> Self {
Self {
weights: vec![0; 26],
}
}
pub(crate) fn addweight(&mut self, c: char) {
self.weights[c as usize - 'a' as usize] += 1;
}
pub(crate) fn setweight(&mut self, c: char, val: isize) {
self.weights[c as usize - 'a' as usize] = val;
}
#[allow(dead_code)]
pub(crate) fn getid(&self, id: usize) -> isize {
self.weights[id]
}
pub(crate) fn getchar(&self, c: char) -> isize {
self.weights[c as usize - 'a' as usize]
}
pub(crate) 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)
}
}
}
}
pub(crate) fn load_words() -> Vec<Word> {
String::from_utf8_lossy(include_bytes!("../wordlists/final.txt"))
.split("\n")
.map(|s| s.into())
.collect()
}
#[derive(Debug, Clone)]
pub(crate) struct Wordlist {
pub(crate) 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 {
pub(crate) fn new(inner: Vec<Word>) -> Self {
Self { inner }
}
pub(crate) fn make_weights(&self) -> Weightlist {
let mut weights = Weightlist::new();
self.inner.iter().for_each(|w| {
let mut found = Vec::new();
w.origin.chars().for_each(|c| {
if !found.contains(&c) {
weights.addweight(c);
found.push(c);
}
});
});
weights
}
pub(crate) fn sort(&mut self) {
self.inner.sort();
}
pub(crate) fn update_weights(&mut self, weights: &Weightlist) {
self.inner.iter_mut().for_each(|w| w.set_weight(weights));
}
pub(crate) fn guess(&self, arg: usize) -> Word {
self.inner.iter().nth_back(arg).unwrap().clone()
}
}
#[derive(Default, Debug, Eq, PartialEq, Clone)]
pub(crate) struct TotalScore {
pub(crate) score: isize,
pub(crate) words: Vec<Word>,
}
impl TotalScore {
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);
}
}
impl Ord for TotalScore {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
match self.score.cmp(&other.score) {
std::cmp::Ordering::Equal => {
if let (Some(w1), Some(w2)) = (self.words.last(), other.words.last()) {
w1.cmp(w2)
} else {
std::cmp::Ordering::Equal
}
}
a => a,
}
}
}
impl PartialOrd for TotalScore {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}