pass clippy pedantic warnings
This commit is contained in:
parent
fe8af22a7d
commit
1adfc443a9
123
src/main.rs
123
src/main.rs
@ -21,18 +21,13 @@ macro_rules! time {
|
||||
|
||||
fn main() {
|
||||
// read normal file if feature "extended-word-list" is not enabled
|
||||
let source;
|
||||
if !cfg!(feature = "extended-word-list") {
|
||||
source = include_str!("wordlewords.csv");
|
||||
} else {
|
||||
source = include_str!("wordlewords-extended.csv");
|
||||
}
|
||||
let source = if cfg!(feature = "extended-word-list") { include_str!("wordlewords-extended.csv") } else { include_str!("wordlewords.csv")};
|
||||
|
||||
// split contents using comma as delimiter
|
||||
let mut words: Vec<String> = source.split(",").map(|s| s.to_string()).collect();
|
||||
let mut words: Vec<String> = source.split(',').map(std::string::ToString::to_string).collect();
|
||||
// remove first and last characters of each word
|
||||
for i in 0..words.len() {
|
||||
words[i] = words[i].trim_matches('"').to_string();
|
||||
for word in &mut words {
|
||||
*word = word.trim_matches('"').to_string();
|
||||
}
|
||||
|
||||
// remove empty strings
|
||||
@ -41,38 +36,28 @@ fn main() {
|
||||
let mut counter = 0;
|
||||
|
||||
let mut before_invalid: Vec<(char, usize)> = Vec::new();
|
||||
println!("g = green, y = yellow, n = gray/none");
|
||||
loop {
|
||||
// ask for word
|
||||
let mut word = get_input(format!("enter word {}: ", counter + 1));
|
||||
let mut word = get_input(format!("enter word {}: ", counter + 1).as_str());
|
||||
word.make_ascii_lowercase();
|
||||
|
||||
// ask for state for each letter
|
||||
// g means the letter is in the correct position
|
||||
// y means the letter is in the wrong position
|
||||
// n means the letter is not in the word
|
||||
let mut state = get_input("state: ".to_owned());
|
||||
let mut state = get_input("state: ".to_owned().as_str());
|
||||
state.make_ascii_lowercase();
|
||||
|
||||
let combination: Vec<(char, char)> = word.chars().zip(state.chars()).collect();
|
||||
|
||||
// get list of incorrect words
|
||||
let mut incorrect_words: Vec<usize> = Vec::with_capacity(words.len());
|
||||
|
||||
// check other words for correctness
|
||||
|
||||
calculate_number_before_invalid(&combination, &mut before_invalid);
|
||||
|
||||
//time in debug mode
|
||||
time!("word filtering", {
|
||||
find_incorrect_words(&combination, &mut incorrect_words, &mut words, &before_invalid);
|
||||
});
|
||||
|
||||
//time in debug mode
|
||||
time!("word removing", {
|
||||
// remove incorrect words from words
|
||||
incorrect_words.reverse();
|
||||
for i in incorrect_words {
|
||||
words.remove(i);
|
||||
}
|
||||
words = find_incorrect_words(&combination, &words, &before_invalid);
|
||||
});
|
||||
|
||||
match words.len() {
|
||||
@ -89,8 +74,8 @@ fn main() {
|
||||
// otherwise print the remaining words
|
||||
_ => {
|
||||
println!("Remaining words:");
|
||||
for i in 0..words.len() {
|
||||
println!("{}", words[i]);
|
||||
for word in &words {
|
||||
println!("{word}");
|
||||
}
|
||||
counter += 1;
|
||||
}
|
||||
@ -101,22 +86,22 @@ fn main() {
|
||||
}
|
||||
|
||||
fn calculate_number_before_invalid(
|
||||
word_state_combination: &Vec<(char, char)>,
|
||||
word_state_combination: &[(char, char)],
|
||||
before_invalid: &mut Vec<(char, usize)>,
|
||||
) {
|
||||
for (letter, state) in word_state_combination.iter() {
|
||||
// if state = n and not already in before_invalid
|
||||
if *state == 'n' && before_invalid.iter().find(|(l, _)| l == letter).is_none() {
|
||||
if *state == 'n' && !before_invalid.iter().any(|(l, _)| l == letter) {
|
||||
// add to before_invalid with a value of how many times it appears in the word with a state of not n
|
||||
let max_value: usize = loop {
|
||||
let max_value: usize = {
|
||||
let mut count = 0;
|
||||
for y in 0..5 {
|
||||
if word_state_combination[y].0 == *letter && word_state_combination[y].1 != 'n'
|
||||
for item in word_state_combination.iter().take(5) {
|
||||
if item.0 == *letter && item.1 != 'n'
|
||||
{
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
break count;
|
||||
count
|
||||
};
|
||||
|
||||
before_invalid.push((*letter, max_value));
|
||||
@ -126,31 +111,28 @@ fn calculate_number_before_invalid(
|
||||
}
|
||||
|
||||
fn find_incorrect_words(
|
||||
combination: &Vec<(char, char)>,
|
||||
incorrect_words: &mut Vec<usize>,
|
||||
combination: &[(char, char)],
|
||||
words: &Vec<String>,
|
||||
before_invalid: &Vec<(char, usize)>,
|
||||
) {
|
||||
for i in 0..words.len() {
|
||||
let i_word = &words[i];
|
||||
'main_loop: for z in 0..5 {
|
||||
let (current_letter, current_state) = combination[z];
|
||||
let other_letter = i_word.chars().nth(z).unwrap();
|
||||
before_invalid: &[(char, usize)],
|
||||
) -> Vec<String> {
|
||||
let mut out = Vec::with_capacity(words.capacity());
|
||||
'wordloop: for word in words {
|
||||
let i_word = word;
|
||||
'main_loop: for (z,item) in combination.iter().enumerate().take(5) {
|
||||
let (current_letter, current_state) = *item;
|
||||
let other_letter = i_word.chars().nth(z).expect("index out of bounds");
|
||||
|
||||
match current_state {
|
||||
'g' => {
|
||||
if other_letter == current_letter {
|
||||
continue 'main_loop;
|
||||
if other_letter != current_letter {
|
||||
continue 'wordloop;
|
||||
}
|
||||
incorrect_words.push(i);
|
||||
break 'main_loop;
|
||||
}
|
||||
'y' => {
|
||||
// if word has a letter in the wrong position add it to incorrect_words
|
||||
if other_letter == current_letter {
|
||||
//println!("Wrong {}", i_word);
|
||||
incorrect_words.push(i);
|
||||
break 'main_loop;
|
||||
continue 'wordloop;
|
||||
}
|
||||
// check if the letter is in the word else add it to incorrect_words
|
||||
for j in i_word.chars() {
|
||||
@ -158,19 +140,17 @@ fn find_incorrect_words(
|
||||
continue 'main_loop;
|
||||
}
|
||||
}
|
||||
incorrect_words.push(i);
|
||||
break 'main_loop;
|
||||
continue 'wordloop;
|
||||
}
|
||||
'n' => {
|
||||
if i_word.chars().filter(|&c| c == current_letter).count()
|
||||
> before_invalid
|
||||
.iter()
|
||||
.find(|(l, _)| l == ¤t_letter)
|
||||
.unwrap()
|
||||
.expect("letter not in before_invalid")
|
||||
.1
|
||||
{
|
||||
incorrect_words.push(i);
|
||||
break 'main_loop;
|
||||
continue 'wordloop;
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
@ -178,31 +158,32 @@ fn find_incorrect_words(
|
||||
}
|
||||
}
|
||||
}
|
||||
out.push(i_word.clone());
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
// get input from user
|
||||
fn get_input(text: String) -> String {
|
||||
fn get_input(text: &str) -> String {
|
||||
use std::io::{stdin, stdout};
|
||||
loop {
|
||||
let mut input = String::new();
|
||||
use std::io::{stdin, stdout};
|
||||
print!("{}", text);
|
||||
print!("{text}");
|
||||
let _ = stdout().flush();
|
||||
stdin()
|
||||
.read_line(&mut input)
|
||||
.expect("Did not enter a correct string");
|
||||
if let Some('\n') = input.chars().next_back() {
|
||||
if input.ends_with('\n') {
|
||||
input.pop();
|
||||
}
|
||||
if let Some('\r') = input.chars().next_back() {
|
||||
if input.ends_with('\r') {
|
||||
input.pop();
|
||||
}
|
||||
let input = input.trim();
|
||||
if input.len() == 5 {
|
||||
return String::from(input);
|
||||
} else {
|
||||
println!("wrong size");
|
||||
}
|
||||
}
|
||||
println!("wrong size");
|
||||
}
|
||||
}
|
||||
|
||||
@ -224,8 +205,6 @@ mod tests {
|
||||
// remove empty strings
|
||||
words.retain(|s| !s.is_empty());
|
||||
|
||||
let mut incorrect_words: Vec<usize> = Vec::with_capacity(words.len());
|
||||
|
||||
let word = "armor";
|
||||
let state = "ggyyn";
|
||||
|
||||
@ -234,21 +213,9 @@ mod tests {
|
||||
let mut before_invalid: Vec<(char, usize)> = Vec::new();
|
||||
super::calculate_number_before_invalid(&combination, &mut before_invalid);
|
||||
|
||||
super::find_incorrect_words(&combination, &mut incorrect_words, &mut words, &before_invalid);
|
||||
let correct_words = loop {
|
||||
let mut tmp = Vec::new();
|
||||
let mut counter = 0;
|
||||
for x in incorrect_words.iter() {
|
||||
while *x != counter {
|
||||
tmp.push(counter);
|
||||
counter +=1;
|
||||
}
|
||||
counter +=1;
|
||||
}
|
||||
break tmp;
|
||||
};
|
||||
println!("{:?}", words[correct_words[0]]);
|
||||
assert!(words[correct_words[0]] == "aroma".to_string());
|
||||
let correct_words = super::find_incorrect_words(&combination, &mut words, &before_invalid);
|
||||
println!("{:?}", correct_words[0]);
|
||||
assert!(correct_words[0] == "aroma".to_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -263,4 +230,4 @@ mod tests {
|
||||
super::calculate_number_before_invalid(&combination, &mut before_invalid);
|
||||
assert_eq!(before_invalid, vec![('g',0),('e',1),('s',0)]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user