1
0
mirror of https://gitlab.com/MisterBiggs/wordle-rs.git synced 2025-06-16 05:56:39 +00:00

begin implementing cli

This commit is contained in:
Anson Biggs 2022-02-07 13:00:40 -07:00
parent 68685da37d
commit 4ded67039a
3 changed files with 64 additions and 27 deletions

9
Cargo.lock generated
View File

@ -2,6 +2,15 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "owo-colors"
version = "3.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "20448fd678ec04e6ea15bbe0476874af65e98a01515d667aa49f1434dc44ebf4"
[[package]]
name = "wordlers"
version = "0.1.0"
dependencies = [
"owo-colors",
]

View File

@ -6,3 +6,4 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
owo-colors = "3.2.0"

View File

@ -5,51 +5,57 @@ fn main() {
.expect("Cannot read file.")
.split('\n')
.filter(|&word| word.trim().len() == 5)
.map(|word| word.trim().to_owned().to_lowercase())
.map(|word| word.trim().to_lowercase())
.collect::<Vec<String>>();
let needed = "kilis";
let banned = "pourane";
// let mut count: usize = 0;
let mut exact = "ak..";
let mut close: Vec<(char, usize)> = vec![('a', 2)];
let mut banned = "ponieu";
words = words
.iter()
.filter(|word| check_valid(word, needed, banned))
.filter(|&word| check_exact(word, exact))
.filter(|&word| check_close(word, &close))
.filter(|&word| check_banned(word, banned))
.map(|word| word.to_owned())
.collect::<Vec<String>>();
let values = words
.iter()
.map(|word| word_value(word))
.collect::<Vec<usize>>();
let &max = values.iter().max().unwrap();
println!("Total Words: {}\nBest Guesses:", words.len());
for (word, value) in words.iter().zip(values) {
if value == max {
println!("{}", word);
}
}
print_guesses(words);
// let mut count: usize = 0;
}
fn check_valid(word: &String, needed: &str, banned: &str) -> bool {
check_banned(word, banned) && check_needed(word, needed)
fn check_exact(word: &String, exact: &str) -> bool {
assert!(
word.len() == exact.len(),
"Exact match string has invalid length!"
);
for (w, e) in word.chars().into_iter().zip(exact.chars()) {
if e == '.' {
continue;
}
fn check_banned(word: &String, banned: &str) -> bool {
for b in banned.chars() {
if word.contains(b) {
if w != e {
return false;
}
}
return true;
}
fn check_needed(word: &String, needed: &str) -> bool {
for need in needed.chars() {
if !word.contains(need) {
fn check_close(word: &String, close: &Vec<(char, usize)>) -> bool {
let mut w = word.chars();
for (character, index) in close {
if character.to_owned() == w.nth(index - 1).unwrap() {
return false;
}
}
return true;
}
fn check_banned(word: &String, banned: &str) -> bool {
for b in banned.chars() {
if word.contains(b) {
return false;
}
}
@ -74,3 +80,24 @@ fn word_value(word: &String) -> usize {
return value;
}
fn print_guesses(words: Vec<String>) {
if words.is_empty() {
println!("Out of guesses!!!");
return;
}
let values = words
.iter()
.map(|word| word_value(word))
.collect::<Vec<usize>>();
let &max = values.iter().max().unwrap();
println!("Total Words: {}\nBest Guesses:", words.len());
for (word, value) in words.iter().zip(values) {
if value == max {
println!("{}", word);
}
}
}