diff --git a/Cargo.lock b/Cargo.lock index 358b73f..294d3b8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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", +] diff --git a/Cargo.toml b/Cargo.toml index 271ac03..0c35b5a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/main.rs b/src/main.rs index 884d80d..50d212f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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::>(); - 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::>(); - let values = words - .iter() - .map(|word| word_value(word)) - .collect::>(); + print_guesses(words); + // let mut count: usize = 0; +} - let &max = values.iter().max().unwrap(); +fn check_exact(word: &String, exact: &str) -> bool { + assert!( + word.len() == exact.len(), + "Exact match string has invalid length!" + ); - println!("Total Words: {}\nBest Guesses:", words.len()); - for (word, value) in words.iter().zip(values) { - if value == max { - println!("{}", word); + for (w, e) in word.chars().into_iter().zip(exact.chars()) { + if e == '.' { + continue; } - } -} -fn check_valid(word: &String, needed: &str, banned: &str) -> bool { - check_banned(word, banned) && check_needed(word, needed) -} - -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) { + if words.is_empty() { + println!("Out of guesses!!!"); + return; + } + + let values = words + .iter() + .map(|word| word_value(word)) + .collect::>(); + + 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); + } + } +}