From 68685da37d539b4defcd980d10990329343315a6 Mon Sep 17 00:00:00 2001 From: MisterBiggs Date: Sun, 6 Feb 2022 16:18:05 -0700 Subject: [PATCH] basic wordle solving capabilities --- src/main.rs | 61 ++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 46 insertions(+), 15 deletions(-) diff --git a/src/main.rs b/src/main.rs index 24c0be5..884d80d 100644 --- a/src/main.rs +++ b/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::>(); + + 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::>(); - let needed = "def"; - let banned = "abc"; + let values = words + .iter() + .map(|word| word_value(word)) + .collect::>(); - 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::() - .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; +}