Better results

This commit is contained in:
Guilleag01
2026-01-08 09:25:09 +01:00
parent fd0972102c
commit 66fed8288e

View File

@@ -72,7 +72,7 @@ fn main() {
stdout().flush().unwrap();
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
} else {
std::cmp::Ordering::Less
@@ -119,8 +119,9 @@ fn get_score(word: &str, words: &[&str], hmap: &mut HashMap<u128, Result>) -> f3
possible += score;
} else {
let c = get_number_rem_words(word, &res, &other_words);
score_hmap.insert((word, res), c as f32 / l as f32);
possible += c as f32 / l as f32;
let s = ((c as f32 / l as f32) - 0.5).abs();
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
}
/// 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 {
let mut count_g: 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 --------------- */
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_get_result_aureo_manga() {
let mut hmap = HashMap::new();
assert_eq!(
get_result(&String::from("aureo"), "manga", &mut hmap),
Result([
LetterState::Yellow,
LetterState::Gray,
LetterState::Gray,
LetterState::Gray,
LetterState::Gray
])
);
}
#[test]
fn test_get_result_aureo_manga() {
let mut hmap = HashMap::new();
assert_eq!(
get_result(&String::from("aureo"), "manga", &mut hmap),
Result([
LetterState::Yellow,
LetterState::Gray,
LetterState::Gray,
LetterState::Gray,
LetterState::Gray
])
);
}
#[test]
fn test_get_result_aabbb_manga() {
let mut hmap = HashMap::new();
assert_eq!(
get_result(&String::from("aabbb"), "manga", &mut hmap),
Result([
LetterState::Yellow,
LetterState::Green,
LetterState::Gray,
LetterState::Gray,
LetterState::Gray
])
);
}
#[test]
fn test_get_result_aabbb_manga() {
let mut hmap = HashMap::new();
assert_eq!(
get_result(&String::from("aabbb"), "manga", &mut hmap),
Result([
LetterState::Yellow,
LetterState::Green,
LetterState::Gray,
LetterState::Gray,
LetterState::Gray
])
);
}
#[test]
fn test_get_result_ababb_manga() {
let mut hmap = HashMap::new();
assert_eq!(
get_result(&String::from("ababb"), "manga", &mut hmap),
Result([
LetterState::Yellow,
LetterState::Gray,
LetterState::Yellow,
LetterState::Gray,
LetterState::Gray
])
);
}
#[test]
fn test_get_result_ababb_manga() {
let mut hmap = HashMap::new();
assert_eq!(
get_result(&String::from("ababb"), "manga", &mut hmap),
Result([
LetterState::Yellow,
LetterState::Gray,
LetterState::Yellow,
LetterState::Gray,
LetterState::Gray
])
);
}
#[test]
fn test_get_result_abaab_manga() {
let mut hmap = HashMap::new();
assert_eq!(
get_result(&String::from("abaab"), "manga", &mut hmap),
Result([
LetterState::Yellow,
LetterState::Gray,
LetterState::Yellow,
LetterState::Gray,
LetterState::Gray
])
);
}
#[test]
fn test_get_result_hucha_mucha() {
let mut hmap = HashMap::new();
assert_eq!(
get_result(&String::from("hucha"), "mucha", &mut hmap),
Result([
LetterState::Gray,
LetterState::Green,
LetterState::Green,
LetterState::Green,
LetterState::Green
])
);
}
#[test]
fn test_get_result_abaab_manga() {
let mut hmap = HashMap::new();
assert_eq!(
get_result(&String::from("abaab"), "manga", &mut hmap),
Result([
LetterState::Yellow,
LetterState::Gray,
LetterState::Yellow,
LetterState::Gray,
LetterState::Gray
])
);
}
#[test]
fn test_get_result_hucha_mucha() {
let mut hmap = HashMap::new();
assert_eq!(
get_result(&String::from("hucha"), "mucha", &mut hmap),
Result([
LetterState::Gray,
LetterState::Green,
LetterState::Green,
LetterState::Green,
LetterState::Green
])
);
}
#[test]
fn test_get_result_gafea_salto() {
let mut hmap = HashMap::new();
assert_eq!(
get_result(&String::from("gafea"), "salto", &mut hmap),
Result([
LetterState::Gray,
LetterState::Green,
LetterState::Gray,
LetterState::Gray,
LetterState::Gray
])
);
}
#[test]
fn test_get_result_gafea_salto() {
let mut hmap = HashMap::new();
assert_eq!(
get_result(&String::from("gafea"), "salto", &mut hmap),
Result([
LetterState::Gray,
LetterState::Green,
LetterState::Gray,
LetterState::Gray,
LetterState::Gray
])
);
}
#[test]
fn test_get_result_botox_salto() {
let mut hmap = HashMap::new();
assert_eq!(
get_result(&String::from("botox"), "salto", &mut hmap),
Result([
LetterState::Gray,
LetterState::Yellow,
LetterState::Yellow,
LetterState::Gray,
LetterState::Gray
])
);
}
#[test]
fn test_get_result_botox_salto() {
let mut hmap = HashMap::new();
assert_eq!(
get_result(&String::from("botox"), "salto", &mut hmap),
Result([
LetterState::Gray,
LetterState::Yellow,
LetterState::Yellow,
LetterState::Gray,
LetterState::Gray
])
);
}
#[test]
fn test_get_result_ulula_valor() {
let mut hmap = HashMap::new();
assert_eq!(
get_result(&String::from("ulula"), "valor", &mut hmap),
Result([
LetterState::Gray,
LetterState::Yellow,
LetterState::Gray,
LetterState::Gray,
LetterState::Yellow
])
);
}
#[test]
fn test_get_result_ulula_valor() {
let mut hmap = HashMap::new();
assert_eq!(
get_result(&String::from("ulula"), "valor", &mut hmap),
Result([
LetterState::Gray,
LetterState::Yellow,
LetterState::Gray,
LetterState::Gray,
LetterState::Yellow
])
);
}
#[test]
fn test_is_possible_hucha_mucha() {
assert!(is_possible(
"hucha",
&Result([
LetterState::Gray,
LetterState::Green,
LetterState::Green,
LetterState::Green,
LetterState::Green
]),
"mucha"
));
}
#[test]
fn test_is_possible_hucha_mucha() {
assert!(is_possible(
"hucha",
&Result([
LetterState::Gray,
LetterState::Green,
LetterState::Green,
LetterState::Green,
LetterState::Green
]),
"mucha"
));
}
#[test]
fn test_is_possible_aureo_bureo() {
assert!(is_possible(
"aureo",
&Result([
LetterState::Gray,
LetterState::Green,
LetterState::Green,
LetterState::Green,
LetterState::Green
]),
"bureo"
));
}
#[test]
fn test_is_possible_aureo_bureo() {
assert!(is_possible(
"aureo",
&Result([
LetterState::Gray,
LetterState::Green,
LetterState::Green,
LetterState::Green,
LetterState::Green
]),
"bureo"
));
}
#[test]
fn test_is_possible_izaba_zureo() {
assert!(!is_possible(
"izaba",
&Result([
LetterState::Gray,
LetterState::Gray,
LetterState::Gray,
LetterState::Gray,
LetterState::Gray
]),
"zureo"
));
}
#[test]
fn test_is_possible_izaba_zureo() {
assert!(!is_possible(
"izaba",
&Result([
LetterState::Gray,
LetterState::Gray,
LetterState::Gray,
LetterState::Gray,
LetterState::Gray
]),
"zureo"
));
}
#[test]
fn test_is_possible_gafea_salto() {
assert!(is_possible(
"gafea",
&Result([
LetterState::Gray,
LetterState::Green,
LetterState::Gray,
LetterState::Gray,
LetterState::Gray
]),
"salto"
));
}
#[test]
fn test_is_possible_gafea_salto() {
assert!(is_possible(
"gafea",
&Result([
LetterState::Gray,
LetterState::Green,
LetterState::Gray,
LetterState::Gray,
LetterState::Gray
]),
"salto"
));
}
#[test]
fn test_is_possible_botox_salto() {
assert!(is_possible(
"botox",
&Result([
LetterState::Gray,
LetterState::Yellow,
LetterState::Yellow,
LetterState::Gray,
LetterState::Gray
]),
"salto"
));
}
#[test]
fn test_is_possible_botox_salto() {
assert!(is_possible(
"botox",
&Result([
LetterState::Gray,
LetterState::Yellow,
LetterState::Yellow,
LetterState::Gray,
LetterState::Gray
]),
"salto"
));
}
#[test]
fn test_is_possible_ulula_valor() {
assert!(is_possible(
"ulula",
&Result([
LetterState::Gray,
LetterState::Yellow,
LetterState::Gray,
LetterState::Gray,
LetterState::Yellow
]),
"valor"
));
#[test]
fn test_is_possible_ulula_valor() {
assert!(is_possible(
"ulula",
&Result([
LetterState::Gray,
LetterState::Yellow,
LetterState::Gray,
LetterState::Gray,
LetterState::Yellow
]),
"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);
}
}