From 6874411672f8383067d3edf0adfc36124b2f9436 Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Sat, 27 Sep 2025 22:10:37 +0200 Subject: [PATCH] Adds/Sub --- src/cli.rs | 10 ++++++++++ src/main.rs | 4 ++-- src/word_representation.rs | 35 ++++++++++++++++++++++++++++------- wordlists/final.txt | 6 ------ 4 files changed, 40 insertions(+), 15 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index b2a1d9f..7222d9b 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -2,6 +2,8 @@ use std::fmt::Display; use clap::{Parser, Subcommand, ValueEnum}; +use crate::word_representation::Penalty; + #[derive(Parser, Debug)] #[command(version, about, long_about = None)] pub(crate) struct Args { @@ -10,6 +12,14 @@ pub(crate) struct Args { #[arg(long, short, value_enum, default_value_t=Wordtypes::Full)] pub(crate) wordlist: Wordtypes, + + /// Duplicate letters are set to this value + #[arg(short, long)] + pub(crate) set: isize, + + /// Duplicate letters instead decrease the value by this amount + #[arg(short, long)] + pub(crate) sub: isize, } #[derive(Subcommand, Debug)] diff --git a/src/main.rs b/src/main.rs index b9b2f9c..4342585 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,7 @@ // #![allow(dead_code)] use crate::cli::Args; use crate::solver::{backtrack_best, create_list_best}; -use crate::word_representation::TotalScore; +use crate::word_representation::{Penalty, TotalScore}; use clap::Parser; use regex::Regex; @@ -19,7 +19,7 @@ fn main_dispatch(args: Args) { cli::Wordtypes::Full => word_representation::load_full_words(), // cli::Wordtypes::Mixed => word_representation::load_full_words(), }); - let weights = wl.make_weights(); + let weights = wl.make_weights(Penalty::Set(0)); match args.command { cli::Commands::Solve { diff --git a/src/word_representation.rs b/src/word_representation.rs index b380129..6e464d9 100644 --- a/src/word_representation.rs +++ b/src/word_representation.rs @@ -44,20 +44,39 @@ impl PartialOrd for Word { #[derive(Default, Eq, PartialEq, Debug, Clone)] pub(crate) struct Weightlist { pub(crate) weights: Vec, + penalty: Penalty, +} + +/// A penalty value +#[derive(Eq, PartialEq, Debug, Clone)] +pub(crate) enum Penalty { + Set(isize), + Add(isize), +} + +impl Default for Penalty { + fn default() -> Self { + Self::Set(0) + } } impl Weightlist { - pub(crate) fn new() -> Self { + pub(crate) fn new(penalty: Penalty) -> Self { Self { weights: vec![0; 26], + penalty, } } - pub(crate) fn addweight(&mut self, c: char) { + + pub(crate) fn incweight(&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; } + pub(crate) fn addweight(&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 { @@ -71,7 +90,10 @@ impl Weightlist { let mut found = vec![]; for letter in guess.origin.chars() { if !found.contains(&letter) { - self.setweight(letter, 0); + match self.penalty { + Penalty::Set(v) => self.setweight(letter, v), + Penalty::Add(v) => self.addweight(letter, v), + } found.push(letter) } } @@ -142,13 +164,13 @@ impl Wordlist { /// 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(); + pub(crate) fn make_weights(&self, penalty: Penalty) -> Weightlist { + let mut weights = Weightlist::new(penalty); self.inner.iter().for_each(|w| { let mut found = Vec::new(); w.origin.chars().for_each(|c| { if !found.contains(&c) { - weights.addweight(c); + weights.incweight(c); found.push(c); } }); @@ -221,7 +243,6 @@ impl TotalScore { } /// Add a word to the score - // TODO: Remove word ? pub(crate) fn add_word(&mut self, word: Word) { self.score += word.weight; self.weights.remove_word(&word); diff --git a/wordlists/final.txt b/wordlists/final.txt index 26db829..184cd57 100644 --- a/wordlists/final.txt +++ b/wordlists/final.txt @@ -836,7 +836,6 @@ arian arias arica ariel -aries arils arioi arion @@ -2810,7 +2809,6 @@ claes clags claik claim -clamb clame clamp clams @@ -3405,7 +3403,6 @@ cylix cymae cymar cymas -cymba cymes cymol cymru @@ -4227,7 +4224,6 @@ dwell dwelt dwine dwyer -dwyka dyads dyaks dyaus @@ -6573,7 +6569,6 @@ hullo hulls hully human -humbo humet humic humid @@ -9820,7 +9815,6 @@ orcin order ordos oread -oreas oreos orfes orgal