Files
wordle-solver-rs/src/app/wordle_solver.rs
T

50 lines
1.4 KiB
Rust

use std::io;
use std::io::Write;
use std::path::Path;
use wordle_solver::{
pattern::{CharStatus, Pattern},
solver::Solver,
word_list::WordList,
};
fn pat_from_str(pat_str: &str) -> Vec<CharStatus> {
pat_str
.chars()
.map(|c| match c {
'g' => CharStatus::GREEN,
'y' => CharStatus::YELLOW,
_ => CharStatus::GREY,
})
.collect()
}
pub fn main() -> io::Result<()> {
let valid_words = WordList::from_file(Path::new("valid-words.txt")).unwrap();
let answers = WordList::from_file(Path::new("answers.txt")).unwrap();
let mut solver = Solver::new(valid_words, answers);
let mut buf = String::new();
loop {
print!("Your guess: ");
io::stdout().flush()?;
buf.clear();
io::stdin().read_line(&mut buf)?;
let guess = buf.trim_ascii().to_ascii_uppercase();
print!("Result [-,y,g]: ");
io::stdout().flush()?;
buf.clear();
io::stdin().read_line(&mut buf)?;
let pat_str = buf.trim_ascii().to_ascii_lowercase();
solver
.apply_guess(Pattern::try_new(&guess, pat_from_str(&pat_str)).unwrap())
.unwrap();
let best_words = solver.evaluate_all_words().unwrap();
println!("\n{}\n", solver.tabular_format());
println!("{}", Solver::best_word_format(best_words, 10));
}
}