feat: refine wasm interfacing
- allow loading wordlist from internal data - no unwraps in wasm branches
This commit is contained in:
+11
-9
@@ -53,13 +53,13 @@ impl Solver {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn apply_guess(&mut self, guess: Pattern) {
|
||||
pub fn apply_guess(&mut self, guess: Pattern) -> Result<(), String> {
|
||||
let new_possible_solutions = self.possible_solutions.apply_guess(&guess);
|
||||
let information_gained = self.possible_solutions.equal_likelieness_entropy()
|
||||
- new_possible_solutions.equal_likelieness_entropy();
|
||||
|
||||
if new_possible_solutions.len() == 0 {
|
||||
panic!("Solution space is empty");
|
||||
return Err("Solution space is empty".to_string());
|
||||
}
|
||||
|
||||
self.possible_solutions = new_possible_solutions;
|
||||
@@ -68,6 +68,8 @@ impl Solver {
|
||||
pattern: guess,
|
||||
information_gained,
|
||||
});
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn estimate_guesses(&self, expected_information_gained: f64) -> f64 {
|
||||
@@ -78,10 +80,10 @@ impl Solver {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn evaluate_word(&self, word: &str) -> WordStats {
|
||||
pub fn evaluate_word(&self, word: &str) -> Result<WordStats, String> {
|
||||
let solution_probability = self.possible_solutions.word_probability(word);
|
||||
|
||||
let expected_information_gained = self.possible_solutions.entropy_if_guessed(word);
|
||||
let expected_information_gained = self.possible_solutions.entropy_if_guessed(word)?;
|
||||
|
||||
let score = (self.guesses.len() + 1) as f64;
|
||||
|
||||
@@ -89,29 +91,29 @@ impl Solver {
|
||||
+ (1.0 - solution_probability)
|
||||
* (score + self.estimate_guesses(expected_information_gained));
|
||||
|
||||
WordStats {
|
||||
Ok(WordStats {
|
||||
word: word.to_string(),
|
||||
solution_probability,
|
||||
expected_information_gained,
|
||||
expected_score_after_guess,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub fn evaluate_all_words(&mut self) -> Vec<WordStats> {
|
||||
pub fn evaluate_all_words(&mut self) -> Result<Vec<WordStats>, String> {
|
||||
self.best_words = self
|
||||
.valid_words
|
||||
.words()
|
||||
.par_bridge()
|
||||
.progress_count(self.valid_words.len() as u64)
|
||||
.map(|w| self.evaluate_word(w))
|
||||
.collect();
|
||||
.collect::<Result<_, String>>()?;
|
||||
|
||||
self.best_words.sort_by(|a, b| {
|
||||
a.expected_score_after_guess
|
||||
.total_cmp(&b.expected_score_after_guess)
|
||||
});
|
||||
|
||||
self.best_words.clone()
|
||||
Ok(self.best_words.clone())
|
||||
}
|
||||
|
||||
pub fn best_word_format(&self, n: usize) -> String {
|
||||
|
||||
Reference in New Issue
Block a user