mirror of
https://gitlab.com/MisterBiggs/wordle-rs.git
synced 2025-08-03 12:11:32 +00:00
basic wordle solving capabilities
This commit is contained in:
61
src/main.rs
61
src/main.rs
@@ -1,18 +1,34 @@
|
||||
use std::fs;
|
||||
|
||||
fn main() {
|
||||
let words = fs::read_to_string("./wordle.txt")
|
||||
let mut words = fs::read_to_string("./wordle.txt")
|
||||
.expect("Cannot read file.")
|
||||
.split('\n')
|
||||
.filter(|&word| word.len() == 5)
|
||||
.filter(|&word| word.trim().len() == 5)
|
||||
.map(|word| word.trim().to_owned().to_lowercase())
|
||||
.collect::<Vec<String>>();
|
||||
|
||||
let needed = "kilis";
|
||||
let banned = "pourane";
|
||||
|
||||
// let mut count: usize = 0;
|
||||
|
||||
words = words
|
||||
.iter()
|
||||
.filter(|word| check_valid(word, needed, banned))
|
||||
.map(|word| word.to_owned())
|
||||
.collect::<Vec<String>>();
|
||||
|
||||
let needed = "def";
|
||||
let banned = "abc";
|
||||
let values = words
|
||||
.iter()
|
||||
.map(|word| word_value(word))
|
||||
.collect::<Vec<usize>>();
|
||||
|
||||
for word in words {
|
||||
if check_valid(&word, needed, banned) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -32,14 +48,29 @@ fn check_banned(word: &String, banned: &str) -> bool {
|
||||
}
|
||||
|
||||
fn check_needed(word: &String, needed: &str) -> bool {
|
||||
needed
|
||||
.chars()
|
||||
.filter(|&n| word.contains(n))
|
||||
.collect::<String>()
|
||||
.len()
|
||||
== 5
|
||||
for need in needed.chars() {
|
||||
if !word.contains(need) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// fn word_value(word: &str) -> usize {
|
||||
// todo!()
|
||||
// }
|
||||
fn word_value(word: &String) -> usize {
|
||||
let mut value: usize = 0;
|
||||
|
||||
// Give value to vowels in word
|
||||
let vowels = "aeiou";
|
||||
for vowel in vowels.chars() {
|
||||
if word.contains(vowel) {
|
||||
value = value + 2;
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: reimplement after needs uses letter index
|
||||
// if word.chars().last().unwrap() == 's' {
|
||||
// value = value + 3;
|
||||
// }
|
||||
|
||||
return value;
|
||||
}
|
||||
|
Reference in New Issue
Block a user