Better results
This commit is contained in:
451
src/main.rs
451
src/main.rs
@@ -72,7 +72,7 @@ fn main() {
|
|||||||
stdout().flush().unwrap();
|
stdout().flush().unwrap();
|
||||||
|
|
||||||
words_score.sort_by(|a, b| {
|
words_score.sort_by(|a, b| {
|
||||||
if (a.1 - 0.5).abs() > (b.1 - 0.5).abs() {
|
if (a.1 - 0.0).abs() > (b.1 - 0.0).abs() {
|
||||||
std::cmp::Ordering::Greater
|
std::cmp::Ordering::Greater
|
||||||
} else {
|
} else {
|
||||||
std::cmp::Ordering::Less
|
std::cmp::Ordering::Less
|
||||||
@@ -119,8 +119,9 @@ fn get_score(word: &str, words: &[&str], hmap: &mut HashMap<u128, Result>) -> f3
|
|||||||
possible += score;
|
possible += score;
|
||||||
} else {
|
} else {
|
||||||
let c = get_number_rem_words(word, &res, &other_words);
|
let c = get_number_rem_words(word, &res, &other_words);
|
||||||
score_hmap.insert((word, res), c as f32 / l as f32);
|
let s = ((c as f32 / l as f32) - 0.5).abs();
|
||||||
possible += c as f32 / l as f32;
|
score_hmap.insert((word, res), s);
|
||||||
|
possible += s; // (c as f32 / l as f32) *
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -197,6 +198,26 @@ fn get_result(guess: &str, real_acc: &str, hmap: &mut HashMap<u128, Result>) ->
|
|||||||
r
|
r
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Checks if the second word passed could be a possible
|
||||||
|
/// solution, given a guessed word and the result.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// let result = is_possible(
|
||||||
|
/// "hucha",
|
||||||
|
/// &Result([
|
||||||
|
/// LetterState::Gray,
|
||||||
|
/// LetterState::Green,
|
||||||
|
/// LetterState::Green,
|
||||||
|
/// LetterState::Green,
|
||||||
|
/// LetterState::Green
|
||||||
|
/// ]),
|
||||||
|
/// "mucha"
|
||||||
|
/// )
|
||||||
|
///
|
||||||
|
/// assert!(result);
|
||||||
|
/// ```
|
||||||
fn is_possible(guessed: &str, result: &Result, to_check: &str) -> bool {
|
fn is_possible(guessed: &str, result: &Result, to_check: &str) -> bool {
|
||||||
let mut count_g: HashMap<char, u8> = HashMap::new();
|
let mut count_g: HashMap<char, u8> = HashMap::new();
|
||||||
let mut count_r: HashMap<char, u8> = HashMap::new();
|
let mut count_r: HashMap<char, u8> = HashMap::new();
|
||||||
@@ -256,212 +277,246 @@ fn hash_words(word: &str, real: &str) -> u128 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* --------------- TESTS --------------- */
|
/* --------------- TESTS --------------- */
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_get_result_aureo_manga() {
|
fn test_get_result_aureo_manga() {
|
||||||
let mut hmap = HashMap::new();
|
let mut hmap = HashMap::new();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
get_result(&String::from("aureo"), "manga", &mut hmap),
|
get_result(&String::from("aureo"), "manga", &mut hmap),
|
||||||
Result([
|
Result([
|
||||||
LetterState::Yellow,
|
LetterState::Yellow,
|
||||||
LetterState::Gray,
|
LetterState::Gray,
|
||||||
LetterState::Gray,
|
LetterState::Gray,
|
||||||
LetterState::Gray,
|
LetterState::Gray,
|
||||||
LetterState::Gray
|
LetterState::Gray
|
||||||
])
|
])
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_get_result_aabbb_manga() {
|
fn test_get_result_aabbb_manga() {
|
||||||
let mut hmap = HashMap::new();
|
let mut hmap = HashMap::new();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
get_result(&String::from("aabbb"), "manga", &mut hmap),
|
get_result(&String::from("aabbb"), "manga", &mut hmap),
|
||||||
Result([
|
Result([
|
||||||
LetterState::Yellow,
|
LetterState::Yellow,
|
||||||
LetterState::Green,
|
LetterState::Green,
|
||||||
LetterState::Gray,
|
LetterState::Gray,
|
||||||
LetterState::Gray,
|
LetterState::Gray,
|
||||||
LetterState::Gray
|
LetterState::Gray
|
||||||
])
|
])
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_get_result_ababb_manga() {
|
fn test_get_result_ababb_manga() {
|
||||||
let mut hmap = HashMap::new();
|
let mut hmap = HashMap::new();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
get_result(&String::from("ababb"), "manga", &mut hmap),
|
get_result(&String::from("ababb"), "manga", &mut hmap),
|
||||||
Result([
|
Result([
|
||||||
LetterState::Yellow,
|
LetterState::Yellow,
|
||||||
LetterState::Gray,
|
LetterState::Gray,
|
||||||
LetterState::Yellow,
|
LetterState::Yellow,
|
||||||
LetterState::Gray,
|
LetterState::Gray,
|
||||||
LetterState::Gray
|
LetterState::Gray
|
||||||
])
|
])
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_get_result_abaab_manga() {
|
fn test_get_result_abaab_manga() {
|
||||||
let mut hmap = HashMap::new();
|
let mut hmap = HashMap::new();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
get_result(&String::from("abaab"), "manga", &mut hmap),
|
get_result(&String::from("abaab"), "manga", &mut hmap),
|
||||||
Result([
|
Result([
|
||||||
LetterState::Yellow,
|
LetterState::Yellow,
|
||||||
LetterState::Gray,
|
LetterState::Gray,
|
||||||
LetterState::Yellow,
|
LetterState::Yellow,
|
||||||
LetterState::Gray,
|
LetterState::Gray,
|
||||||
LetterState::Gray
|
LetterState::Gray
|
||||||
])
|
])
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn test_get_result_hucha_mucha() {
|
fn test_get_result_hucha_mucha() {
|
||||||
let mut hmap = HashMap::new();
|
let mut hmap = HashMap::new();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
get_result(&String::from("hucha"), "mucha", &mut hmap),
|
get_result(&String::from("hucha"), "mucha", &mut hmap),
|
||||||
Result([
|
Result([
|
||||||
LetterState::Gray,
|
LetterState::Gray,
|
||||||
LetterState::Green,
|
LetterState::Green,
|
||||||
LetterState::Green,
|
LetterState::Green,
|
||||||
LetterState::Green,
|
LetterState::Green,
|
||||||
LetterState::Green
|
LetterState::Green
|
||||||
])
|
])
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_get_result_gafea_salto() {
|
fn test_get_result_gafea_salto() {
|
||||||
let mut hmap = HashMap::new();
|
let mut hmap = HashMap::new();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
get_result(&String::from("gafea"), "salto", &mut hmap),
|
get_result(&String::from("gafea"), "salto", &mut hmap),
|
||||||
Result([
|
Result([
|
||||||
LetterState::Gray,
|
LetterState::Gray,
|
||||||
LetterState::Green,
|
LetterState::Green,
|
||||||
LetterState::Gray,
|
LetterState::Gray,
|
||||||
LetterState::Gray,
|
LetterState::Gray,
|
||||||
LetterState::Gray
|
LetterState::Gray
|
||||||
])
|
])
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_get_result_botox_salto() {
|
fn test_get_result_botox_salto() {
|
||||||
let mut hmap = HashMap::new();
|
let mut hmap = HashMap::new();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
get_result(&String::from("botox"), "salto", &mut hmap),
|
get_result(&String::from("botox"), "salto", &mut hmap),
|
||||||
Result([
|
Result([
|
||||||
LetterState::Gray,
|
LetterState::Gray,
|
||||||
LetterState::Yellow,
|
LetterState::Yellow,
|
||||||
LetterState::Yellow,
|
LetterState::Yellow,
|
||||||
LetterState::Gray,
|
LetterState::Gray,
|
||||||
LetterState::Gray
|
LetterState::Gray
|
||||||
])
|
])
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_get_result_ulula_valor() {
|
fn test_get_result_ulula_valor() {
|
||||||
let mut hmap = HashMap::new();
|
let mut hmap = HashMap::new();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
get_result(&String::from("ulula"), "valor", &mut hmap),
|
get_result(&String::from("ulula"), "valor", &mut hmap),
|
||||||
Result([
|
Result([
|
||||||
LetterState::Gray,
|
LetterState::Gray,
|
||||||
LetterState::Yellow,
|
LetterState::Yellow,
|
||||||
LetterState::Gray,
|
LetterState::Gray,
|
||||||
LetterState::Gray,
|
LetterState::Gray,
|
||||||
LetterState::Yellow
|
LetterState::Yellow
|
||||||
])
|
])
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_is_possible_hucha_mucha() {
|
fn test_is_possible_hucha_mucha() {
|
||||||
assert!(is_possible(
|
assert!(is_possible(
|
||||||
"hucha",
|
"hucha",
|
||||||
&Result([
|
&Result([
|
||||||
LetterState::Gray,
|
LetterState::Gray,
|
||||||
LetterState::Green,
|
LetterState::Green,
|
||||||
LetterState::Green,
|
LetterState::Green,
|
||||||
LetterState::Green,
|
LetterState::Green,
|
||||||
LetterState::Green
|
LetterState::Green
|
||||||
]),
|
]),
|
||||||
"mucha"
|
"mucha"
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_is_possible_aureo_bureo() {
|
fn test_is_possible_aureo_bureo() {
|
||||||
assert!(is_possible(
|
assert!(is_possible(
|
||||||
"aureo",
|
"aureo",
|
||||||
&Result([
|
&Result([
|
||||||
LetterState::Gray,
|
LetterState::Gray,
|
||||||
LetterState::Green,
|
LetterState::Green,
|
||||||
LetterState::Green,
|
LetterState::Green,
|
||||||
LetterState::Green,
|
LetterState::Green,
|
||||||
LetterState::Green
|
LetterState::Green
|
||||||
]),
|
]),
|
||||||
"bureo"
|
"bureo"
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_is_possible_izaba_zureo() {
|
fn test_is_possible_izaba_zureo() {
|
||||||
assert!(!is_possible(
|
assert!(!is_possible(
|
||||||
"izaba",
|
"izaba",
|
||||||
&Result([
|
&Result([
|
||||||
LetterState::Gray,
|
LetterState::Gray,
|
||||||
LetterState::Gray,
|
LetterState::Gray,
|
||||||
LetterState::Gray,
|
LetterState::Gray,
|
||||||
LetterState::Gray,
|
LetterState::Gray,
|
||||||
LetterState::Gray
|
LetterState::Gray
|
||||||
]),
|
]),
|
||||||
"zureo"
|
"zureo"
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_is_possible_gafea_salto() {
|
fn test_is_possible_gafea_salto() {
|
||||||
assert!(is_possible(
|
assert!(is_possible(
|
||||||
"gafea",
|
"gafea",
|
||||||
&Result([
|
&Result([
|
||||||
LetterState::Gray,
|
LetterState::Gray,
|
||||||
LetterState::Green,
|
LetterState::Green,
|
||||||
LetterState::Gray,
|
LetterState::Gray,
|
||||||
LetterState::Gray,
|
LetterState::Gray,
|
||||||
LetterState::Gray
|
LetterState::Gray
|
||||||
]),
|
]),
|
||||||
"salto"
|
"salto"
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_is_possible_botox_salto() {
|
fn test_is_possible_botox_salto() {
|
||||||
assert!(is_possible(
|
assert!(is_possible(
|
||||||
"botox",
|
"botox",
|
||||||
&Result([
|
&Result([
|
||||||
LetterState::Gray,
|
LetterState::Gray,
|
||||||
LetterState::Yellow,
|
LetterState::Yellow,
|
||||||
LetterState::Yellow,
|
LetterState::Yellow,
|
||||||
LetterState::Gray,
|
LetterState::Gray,
|
||||||
LetterState::Gray
|
LetterState::Gray
|
||||||
]),
|
]),
|
||||||
"salto"
|
"salto"
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_is_possible_ulula_valor() {
|
fn test_is_possible_ulula_valor() {
|
||||||
assert!(is_possible(
|
assert!(is_possible(
|
||||||
"ulula",
|
"ulula",
|
||||||
&Result([
|
&Result([
|
||||||
LetterState::Gray,
|
LetterState::Gray,
|
||||||
LetterState::Yellow,
|
LetterState::Yellow,
|
||||||
LetterState::Gray,
|
LetterState::Gray,
|
||||||
LetterState::Gray,
|
LetterState::Gray,
|
||||||
LetterState::Yellow
|
LetterState::Yellow
|
||||||
]),
|
]),
|
||||||
"valor"
|
"valor"
|
||||||
));
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_remaining_words_opaco() {
|
||||||
|
let mut all_words: Vec<&str> = HashSet::from(WORDS).iter().copied().collect();
|
||||||
|
get_remaining_words(
|
||||||
|
"opaco",
|
||||||
|
&Result([
|
||||||
|
LetterState::Gray,
|
||||||
|
LetterState::Yellow,
|
||||||
|
LetterState::Gray,
|
||||||
|
LetterState::Gray,
|
||||||
|
LetterState::Yellow,
|
||||||
|
]),
|
||||||
|
&mut all_words,
|
||||||
|
);
|
||||||
|
let l1 = all_words.len();
|
||||||
|
get_remaining_words(
|
||||||
|
"opaco",
|
||||||
|
&Result([
|
||||||
|
LetterState::Gray,
|
||||||
|
LetterState::Yellow,
|
||||||
|
LetterState::Gray,
|
||||||
|
LetterState::Gray,
|
||||||
|
LetterState::Yellow,
|
||||||
|
]),
|
||||||
|
&mut all_words,
|
||||||
|
);
|
||||||
|
let l2 = all_words.len();
|
||||||
|
assert_eq!(l1, l2);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user